Repository: django/django
File: django/db/models/constraints.py (Python)

Code before:
        else:
            self.violation_error_message = self.default_violation_error_message

    @property
    def contains_expressions(self):
        return False

    def constraint_sql(self, model, schema_editor):
        raise NotImplementedError("This method must be implemented by a subclass.")

    def create_sql(self, model, schema_editor):
        raise NotImplementedError("This method must be implemented by a subclass.")

    def remove_sql(self, model, schema_editor):
        raise NotImplementedError("This method must be implemented by a subclass.")

    @classmethod
    def _expression_refs_exclude(cls, model, expression, exclude):
        get_field = model._meta.get_field
        for field_name, *__ in model._get_expr_references(expression):
            field = get_field(field_name)
            if field.name in exclude:
                return True
            if field.generated and cls._expression_refs_exclude(
                model, field.expression, exclude
            ):
                return True
        return False

    def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS):
        raise NotImplementedError("This method must be implemented by a subclass.")

    def get_violation_error_message(self):
        return self.violation_error_message % {"name": self.name}

    def check(self, model, connection):
        return []

    def _check_references(self, model, references):
        from django.db.models.fields.composite import CompositePrimaryKey

        errors = []
        fields = set()
        for field_name, *lookups in references:
            # pk is an alias that won't be found by opts.get_field().
            if field_name != "pk" or isinstance(model._meta.pk, CompositePrimaryKey):
                fields.add(field_name)
            if not lookups:
                # If it has no lookups it cannot result in a JOIN.
                continue
            try:

Review comment (suggestion):
We need to handle `pk` separately (ticket-27944):
```python
if field_name == "pk":
    field = model._meta.pk
else:
    field = get_field(field_name)
if field.name in exclude:
    return True
```

Code after:
        else:
            self.violation_error_message = self.default_violation_error_message

    @property
    def contains_expressions(self):
        return False

    def constraint_sql(self, model, schema_editor):
        raise NotImplementedError("This method must be implemented by a subclass.")

    def create_sql(self, model, schema_editor):
        raise NotImplementedError("This method must be implemented by a subclass.")

    def remove_sql(self, model, schema_editor):
        raise NotImplementedError("This method must be implemented by a subclass.")

    @classmethod
    def _expression_refs_exclude(cls, model, expression, exclude):
        get_field = model._meta.get_field
        for field_name, *__ in model._get_expr_references(expression):
            if field_name == "pk":
                field = model._meta.pk
            else:
                field = get_field(field_name)
            if field_name in exclude or field.name in exclude:
                return True
            if field.generated and cls._expression_refs_exclude(
                model, field.expression, exclude
            ):
                return True
        return False

    def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS):
        raise NotImplementedError("This method must be implemented by a subclass.")

    def get_violation_error_message(self):
        return self.violation_error_message % {"name": self.name}

    def check(self, model, connection):
        return []

    def _check_references(self, model, references):
        from django.db.models.fields.composite import CompositePrimaryKey

        errors = []
        fields = set()
        for field_name, *lookups in references:
            # pk is an alias that won't be found by opts.get_field().
            if field_name != "pk" or isinstance(model._meta.pk, CompositePrimaryKey):
                fields.add(field_name)
            if not lookups: