Database Tips : Email Unique untuk Softdeletes

Terkadang mau bikin column email yang unique, tapi bakal jadi masalah kalau kita menggunakan softdeletes fitur

solusinya dengan menggunakan partial index berikut ini

 

ALTER TABLE users ADD COLUMN email_unique varchar(512) GENERATED ALWAYS AS (CONCAT(email, '#', IF(deleted_at IS NULL, '-', deleted_at))) VIRTUAL;
CREATE UNIQUE INDEX email_unique ON users (email_unique);

 

https://www.itsolutionstuff.com/post/laravel-unique-validation-with-soft-delete-exampleexample.html

Sumber :

https://twitter.com/tobias_petry/status/1454085321180819457/photo/1













Leave a Reply

Your email address will not be published. Required fields are marked *