Hi,

I changed several models from fields using `unique=True` to using 
`UniqueConstraint` with a condition in the Meta.

As a side-effect, the uniqueness are no longer validated during cleaning of 
a Form and an integrity error is raised. This is because partial unique 
indexes are excluded :
https://github.com/django/django/blob/e703b152c6148ddda1b072a4353e9a41dca87f90/django/db/models/options.py#L865-L874

It seems that `total_unique_constraints` is also used to check for fields 
that should be unique (related fields and USERNAME_FIELD specifically).

I tried modifying `total_unique_constraints` and the only tests which 
failed were related to the above concern and 
`test_total_ordering_optimization_meta_constraints` which also uses `
total_unique_constraints`. My application works fine and the validation 
error are correctly raised in my forms.

The current behaviour of `Model.validate_unique` is also not the one I 
expected as my conditional `UniqueConstraint` were not used (which caused 
the integrity error).

Am I missing something? Or should we use all constraints (including 
partial) in `Model.validate_unique`?

If this is indeed what should be done, adding an `all_unique_constraints` 
next to `total_unique_constraints` and using it in `Model.validate_unique` 
instead of `total_unique_constraints` would do the trick. I don't mind 
opening a ticket and doing the PR if needed.

Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/09c08268-e094-4152-94e2-265d93481891n%40googlegroups.com.

Reply via email to