#34805 closed Uncategorized (invalid)

'Q' object has no attribute 'replace_expressions'

Reported by: Petr Přikryl Owned by: nobody
Component: Database layer (models, ORM) Version: 4.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi, I have problem with calling full_clean on Model object with following constraint containing Q:

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

Change History (1)

comment:1 by Mariusz Felisiak, 14 months ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: invalid
Status: newclosed

Your constraint definition is invalid, you cannot pass Q objects in expressions, check out docs. If you're having trouble understanding how Django works, see TicketClosingReasons/UseSupportChannels for ways to get help.

Note: See TracTickets for help on using tickets.
Back to Top