1
0
mirror of https://github.com/django/django.git synced 2025-03-06 15:32:33 +00:00

Fixed #36092 -- Disallowed non-local fields in composite primary keys.

This commit is contained in:
Bendeguz Csirmaz 2025-01-13 19:33:47 +08:00 committed by Sarah Boyce
parent bf7b17d16d
commit d83fb782d3
2 changed files with 23 additions and 0 deletions

View File

@ -1803,6 +1803,8 @@ class Model(AltersData, metaclass=ModelBase):
hint = f"{field_name!r} field may not set 'null=True'."
elif field.generated:
hint = f"{field_name!r} field is a generated field."
elif field not in meta.local_fields:
hint = f"{field_name!r} field is not a local field."
else:
seen_columns[field.column].append(field_name)

View File

@ -247,3 +247,24 @@ class CompositePKChecksTests(TestCase):
),
],
)
def test_composite_pk_cannot_include_non_local_field(self):
class Foo(models.Model):
a = models.SmallIntegerField()
class Bar(Foo):
pk = models.CompositePrimaryKey("a", "b")
b = models.SmallIntegerField()
self.assertEqual(Foo.check(databases=self.databases), [])
self.assertEqual(
Bar.check(databases=self.databases),
[
checks.Error(
"'a' cannot be included in the composite primary key.",
hint="'a' field is not a local field.",
obj=Bar,
id="models.E042",
),
],
)