{{{
models.UniqueConstraint(
models.Q(color__isnull=True),
"product",
condition=models.Q(color__isnull=True),
name="unique_product_color_null",
),
}}}
Constraint is allowing only one NULL and works well. More at
[https://www.enterprisedb.com/postgres-tutorials/postgresql-unique-
constraint-null-allowing-only-one-null]
Traceback
{{{
/usr/local/lib/python3.11/site-packages/django/db/models/base.py:1497: in
full_clean
self.validate_constraints(exclude=exclude)
/usr/local/lib/python3.11/site-packages/django/db/models/base.py:1445: in
validate_constraints
constraint.validate(model_class, self, exclude=exclude, using=using)
/usr/local/lib/python3.11/site-
packages/django/db/models/constraints.py:346: AttributeError
> expressions.append(Exact(expr,
expr.replace_expressions(replacements)))
E AttributeError: 'Q' object has no attribute
'replace_expressions'
}}}
https://github.com/django/django/blob/4.2.4/django/db/models/constraints.py#L346
--
Ticket URL: <https://code.djangoproject.com/ticket/34805>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => invalid
* component: Uncategorized => Database layer (models, ORM)
Comment:
Your constraint definition is invalid, you cannot pass `Q` objects in
`expressions`, check out
[https://docs.djangoproject.com/en/4.2/ref/models/constraints/#expressions
docs]. If you're having trouble understanding how Django works, see
TicketClosingReasons/UseSupportChannels for ways to get help.
--
Ticket URL: <https://code.djangoproject.com/ticket/34805#comment:1>