# ExclusionConstraint with expression does not apply migration

**URL:** <https://forum.djangoproject.com/t/exclusionconstraint-with-expression-does-not-apply-migration/21574>\
**Category:** Using the ORM\
**Created:** [June 13, 2023, 9:58am UTC](https://forum.djangoproject.com/t/exclusionconstraint-with-expression-does-not-apply-migration/21574 "2023-06-13T09:58:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vazark](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vazark/32/5827_2.png) [@vazark](https://forum.djangoproject.com/u/vazark)\
**Post date:** [June 13, 2023, 9:58am UTC](https://forum.djangoproject.com/t/exclusionconstraint-with-expression-does-not-apply-migration/21574/1 "2023-06-13T09:58:39Z")

</div>

Hello,

I’m setting up an exclusion constraint on through model to ensure that we do not accidentally add multiple offerings with overlapping products to an agency. However when i use “reference\_\_fk\_id”, I get the error “fk\_id” does not exist.

```auto

class AgenceSubscription(models.Model):
    agence = models.ForeignKey(Agence, on_delete=models.CASCADE)
    sub_group = models.ForeignKey(SubscriptionGroup, on_delete=models.CASCADE)

    class Meta:
        ordering = ["agence"]
        constraints = [
            ExclusionConstraint(
                name="exclude_overlapping_subscriptions",
                expressions=[
                    ("agence", RangeOperators.EQUAL),
                    ("sub_group__product_config", RangeOperators.EQUAL),
                ],
            ),
        ]

```

here `product_config` is a FK to another table.

Aren’t expression allowed on ExclusionConstraint ? Or is this not supported?

---

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [June 13, 2023, 10:41pm UTC](https://forum.djangoproject.com/t/exclusionconstraint-with-expression-does-not-apply-migration/21574/2 "2023-06-13T22:41:37Z")

</div>

👋 @vazark !

Database constraints cannot span across tables.

[We have system checks in place](https://docs.djangoproject.com/en/4.2/ref/checks/#models) to ensure that built-in constraint classes (e.g. `CheckConstraints` and `UniqueConstraints`) [respect this limitation](https://github.com/django/django/blob/68d0f95262c83adebd9ec3a416d53d8d54ada166/django/db/models/base.py#L2462-L2477) but since is a contrib abstraction `ExclusionConstraint` uses a different format to store reference to expressions it’s not covered by them.

In order so solve this issue I believe that `Model._check_constraints` should be changed to call a new `Constraint.check` method that encapsulate all the logic required to validate each `Constraint` subclasses. This new entrypoint could then be used by `ExclusionConstraint` to validate the members of its specialized `expressions` collection.
