W
W
WebDev2019-07-22 10:47:51
MySQL
WebDev, 2019-07-22 10:47:51

How to do soft delete and use unique keys?

I have a books table that has a unique alias field.
I'm doing a "soft" deletion, now the deleted_at field has been added, and so that deleted books do not interfere with creating new ones with the same alias, I set a unique key to two fields: alias, deleted_at.
Now you can create new entries with the same alias, even if there is such a "deleted" entry.
But now there is a new problem: deleted_at is null by default, so you can create records with the same alias as many times as you want.
How to keep records unique?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Gomonov, 2019-07-22
@kirill-93

I wouldn't use deleted_at as a flag that the entity has been deleted - it's more of a reference.
Would start the is_delete field

A
Alex, 2019-07-22
@streetflush

Default value deleted_at

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question