﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34149	"Postgres ""ExclusionConstraint with conditions cannot be deferred."" seems wrong"	Márton Salomváry	Márton Salomváry	"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
"	Bug	closed	contrib.postgres	4.1	Normal	fixed		Lily Foote	Ready for checkin	1	0	0	0	0	0
