Hello
Using Django 5.2 and PSQL 16.4
I have a model defined like so:
class Foo(models.Model):
field_A = models.Charfield(blank=True, default=””)
field_B = models.ForeignKey(Hello, null=True, blank=True)
field_C = models.ForeignKey(World, null=True, blank=True)
field_D = models.Charfield(blank=True, default=””)
value_in_need_to_update = models.FloatField()
class Meta:
constraints = [
models.UniqueConstraint(
fields=[
"field_A",
"field_C",
],
name="unique_AC",
),
models.UniqueConstraint(
fields=[
"field_A",
"field_B",
"field_D",
],
condition=~models.Q( # Here the condition is important
field_D="",
),
name="unique_ABD",
),
]
My 2nd unique constraint is conditional: this tuple (A, B, D) must be unique only if D isn’t ””
In my data pipeline I do this:
Foo.objects.bulk_create(
objs,
update_conflicts=True,
update_fields=["value_in_need_to_update"],
unique_fields=[ # The "unique_ABD constraint"
"field_A",
"field_B",
"field_D",
]
)
However Django throws me
...
psycopg.errors.InvalidColumnReference: there is no unique or exclusion constraint matching the ON CONFLICT specification
...
django.db.utils.ProgrammingError: there is no unique or exclusion constraint matching the ON CONFLICT specification
It seems that the conditional UNIQUE constraint isn’t found ? Am I missing something ?
Thank you very much