makinacorpus / django-safedelete

Mask your objects instead of deleting them from your database.
https://django-safedelete.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
677 stars 122 forks source link

Fields uniqueness with MySQL #227

Open kietheros opened 1 year ago

kietheros commented 1 year ago

constraints = [ UniqueConstraint( fields=['name'], condition=Q(deleted__isnull=True), name='unique_active_name' ), ]

MySQL does not support unique constraints with conditions.

If field deleted were bigint: 0 = active, others = deleted, I could create an unique constraint on name and deleted. However, deleted is datetime, null = active, unique (name, deleted) will not work.

Could anyone help me ???

Gagaro commented 1 year ago

I don't know enough about MySQL to help you there, but I used to have a mixin which would change the value in the unique column to some random things when it was soft deleted so we could re-use the value. Of course you can't undelete it and have the value back that way, unless you have another non-unique column to backup the original value.

kietheros commented 1 year ago

Thank for your reply. Currently, I dont use undelete feature and cascading soft delete. I decided to write my simple soft delete model with deleted is integer.