{{{
ExclusionConstraint(
name="my_constraint",
expressions=[
("is_default", RangeOperators.EQUAL),
],
condition=Q(is_default=False),
deferrable=Deferrable.DEFERRED,
)
}}}
`makemigrations` fails with the following error:
{{{
ExclusionConstraint(
File "django/contrib/postgres/constraints.py", line 55, in __init__
raise ValueError("ExclusionConstraint with conditions cannot be
deferred.")
ValueError: ExclusionConstraint with conditions cannot be deferred.
}}}
However PostgreSQL 14.5 seems to be perfectly fine with the following:
{{{
ALTER TABLE my_model
ADD CONSTRAINT my_constraint
EXCLUDE (is_default WITH =)
WHERE (is_default = TRUE)
INITIALLY DEFERRED;
}}}
I've tried to figure out why this restriction may be in place but neither
other parts of Django nor PostgreSQL itself seem to require deferred
exclusion constraints to not have a condition.
The commit that added this also does not seem to explain the situation:
https://github.com/django/django/commit/b4068bc65636cca6c2905aa8c40bea69bb0e4245
Maybe an accidental copypaste from here?
https://github.com/django/django/blob/35911078fa40eb35859832987fedada76963c01e/django/db/models/constraints.py#L153-L154
--
Ticket URL: <https://code.djangoproject.com/ticket/34149>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
Comment:
I think this is effectively a bug due to copy-pasting.
Django implements partial unique constraints using partial unique indices
which
[https://github.com/django/django/blob/35911078fa40eb35859832987fedada76963c01e/django/db/models/constraints.py#L153-L160
lack support for the constraint specific options].
Exclusion constraint use proper `CONSTRAINT` syntax so I don't see why
they couldn't support deferral specifiers.
Would you be willing to submit a patch to correct the issue?
--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:1>
* owner: (none) => Bhuvnesh
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:2>
* cc: Lily Foote (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:3>
Comment (by Márton Salomváry):
Opened a pull request here: https://github.com/django/django/pull/16278
--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:4>
* owner: Bhuvnesh => Márton Salomváry
--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:5>
* has_patch: 0 => 1
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:6>
* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:7>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"d6cbf39a1ba2053c6211a16ed42d5410e5c5cdd0" d6cbf39]:
{{{
#!CommitTicketReference repository=""
revision="d6cbf39a1ba2053c6211a16ed42d5410e5c5cdd0"
Fixed #34149 -- Allowed adding deferrable conditional exclusion
constraints on PostgreSQL.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:8>
Comment (by Simon Charette):
Bhuvnesh, it's great that you are enthusiastic about fixing issues but you
might want to give reporters a chance to reply on whether or not they want
to provide a patch themselves before assigning the ticket to you.
--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:9>