Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fields uniqueness with MySQL #227

Open
kietheros opened this issue Mar 2, 2023 · 2 comments
Open

Fields uniqueness with MySQL #227

kietheros opened this issue Mar 2, 2023 · 2 comments

Comments

@kietheros
Copy link

kietheros commented Mar 2, 2023

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
Copy link
Member

Gagaro commented Mar 6, 2023

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants