[Django] #34149: Postgres "ExclusionConstraint with conditions cannot be deferred." seems wrong

24 views
Skip to first unread message

Django

unread,
Nov 9, 2022, 12:39:43 PM11/9/22
to django-...@googlegroups.com
#34149: Postgres "ExclusionConstraint with conditions cannot be deferred." seems
wrong
--------------------------------------------+------------------------
Reporter: Márton Salomváry | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 4.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
When attempting to add a constraint to a model that looks something like
this:


{{{
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.

Django

unread,
Nov 9, 2022, 9:40:55 PM11/9/22
to django-...@googlegroups.com
#34149: Postgres "ExclusionConstraint with conditions cannot be deferred." seems
wrong
----------------------------------+------------------------------------

Reporter: Márton Salomváry | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Simon Charette):

* 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>

Django

unread,
Nov 9, 2022, 10:30:35 PM11/9/22
to django-...@googlegroups.com
#34149: Postgres "ExclusionConstraint with conditions cannot be deferred." seems
wrong
----------------------------------+------------------------------------
Reporter: Márton Salomváry | Owner: Bhuvnesh
Type: Bug | Status: assigned

Component: contrib.postgres | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Bhuvnesh):

* owner: (none) => Bhuvnesh
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:2>

Django

unread,
Nov 9, 2022, 11:35:17 PM11/9/22
to django-...@googlegroups.com
#34149: Postgres "ExclusionConstraint with conditions cannot be deferred." seems
wrong
----------------------------------+------------------------------------
Reporter: Márton Salomváry | Owner: Bhuvnesh
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* cc: Lily Foote (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:3>

Django

unread,
Nov 10, 2022, 2:32:32 AM11/10/22
to django-...@googlegroups.com
#34149: Postgres "ExclusionConstraint with conditions cannot be deferred." seems
wrong
----------------------------------+------------------------------------
Reporter: Márton Salomváry | Owner: Bhuvnesh
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------

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>

Django

unread,
Nov 10, 2022, 2:34:32 AM11/10/22
to django-...@googlegroups.com
#34149: Postgres "ExclusionConstraint with conditions cannot be deferred." seems
wrong
-------------------------------------+-------------------------------------
Reporter: Márton Salomváry | Owner: Márton
| Salomváry

Type: Bug | Status: assigned
Component: contrib.postgres | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Márton Salomváry):

* owner: Bhuvnesh => Márton Salomváry


--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:5>

Django

unread,
Nov 10, 2022, 2:58:23 AM11/10/22
to django-...@googlegroups.com
#34149: Postgres "ExclusionConstraint with conditions cannot be deferred." seems
wrong
-------------------------------------+-------------------------------------
Reporter: Márton Salomváry | Owner: Márton
| Salomváry
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* has_patch: 0 => 1
* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:6>

Django

unread,
Nov 10, 2022, 5:08:29 AM11/10/22
to django-...@googlegroups.com
#34149: Postgres "ExclusionConstraint with conditions cannot be deferred." seems
wrong
-------------------------------------+-------------------------------------
Reporter: Márton Salomváry | Owner: Márton
| Salomváry
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/34149#comment:7>

Django

unread,
Nov 10, 2022, 5:38:08 AM11/10/22
to django-...@googlegroups.com
#34149: Postgres "ExclusionConstraint with conditions cannot be deferred." seems
wrong
-------------------------------------+-------------------------------------
Reporter: Márton Salomváry | Owner: Márton
| Salomváry
Type: Bug | Status: closed
Component: contrib.postgres | Version: 4.1
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Django

unread,
Nov 10, 2022, 10:21:27 AM11/10/22
to django-...@googlegroups.com
#34149: Postgres "ExclusionConstraint with conditions cannot be deferred." seems
wrong
-------------------------------------+-------------------------------------
Reporter: Márton Salomváry | Owner: Márton
| Salomváry
Type: Bug | Status: closed
Component: contrib.postgres | Version: 4.1
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages