UniqueTogetherValidator fails validating partial unique constraint when there is a nullable FK #9777
-
|
In one of my projects I have a model which defines a set of nullable foreign-keys and a value which is unique according to how FKs are defined, like the following: class UniqueConstraintWithRelationModel(models.Model):
value = models.IntegerField()
first_related = models.ForeignKey(FistModel, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
second_related = models.ForeignKey(SecondModel, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
class Meta:
constraints = [
models.UniqueConstraint(
fields=('value', 'first_related'),
condition=models.Q(second_related__isnull=True),
name='unique_constraint_with_relationa',
),
models.UniqueConstraint(
fields=('value', 'second_related'),
condition=models.Q(first_related__isnull=True),
name='unique_constraint_with_relationb',
),
]When validating this model using a Here is a commit which adds a test case which fails with the current implementation: a4a3093 By debugging the problem seems to be how the query is build, because when executing the same query as in This problems forces me to rewrite the validation for the constraint which results in an extra query which is performed against the database. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
|
is this PR related #9766 ? |
Beta Was this translation helpful? Give feedback.
-
|
PS: in my project the setup is a bit more complex because I have 4 FKs (A,B,C,D) which should form two families of unique sets (A,B,C) when D is NULL and (A,B,D) when C is NULL. In my branch I have reduced to two FK with a value for simplicity. |
Beta Was this translation helpful? Give feedback.
-
|
I might be wrong but here are my thoughts but please let me know if i'm missing something:
|
Beta Was this translation helpful? Give feedback.
-
|
Hi @p-r-a-v-i-n, let me answer to your questions:
This is not an issue which regards the migration system, migrations work fine and this is not an issue. This is a misleading way to view this issue.
The
No, because that would cause the unique-indexes to also account for NULL values which would be useless in my case. The problem is that the ORM/forms can handle this situation yet DRF is not able to validate it. |
Beta Was this translation helpful? Give feedback.
-
|
Hi @sevdog , Your investigation was right, no doubt.
I vaguely remember about this , not sure , was it giving me warning or stopping me to migrate. please ignore this.
TIL! Thanks for the this, I didn't know about this.
for this situation , when I read , instantly i thought using so please forgive me here, I might have shifted the discussion to off side. |
Beta Was this translation helpful? Give feedback.

Hi @p-r-a-v-i-n, let me answer to your questions:
This is not an issue which regards the migration system, migrations work fine and this is not an issue. This is a misleading way to view this issue.
The
unique_togethermay be deprecated in the future (see django docs) and it is way less flexible thanUniqueConstraintlike in this case where there is a condition. To be completed in my code there is also an other condition whi…