Skip to content

Commit cbdf128

Browse files
h4ljacobtylerwalls
authored andcommitted
[5.2.x] Fixed #36704 -- Fixed system check error for proxy model with a composite pk.
Proxy models subclassing a model with a CompositePrimaryKey were incorrectly reporting check errors because the check that requires only local fields to be used in a composite pk was evaluated against the proxy subclass, which has no fields. To fix this, composite pk field checks are not evaluated against proxy subclasses, as none of the checks are applicable to proxy subclasses. This also has the benefit of not double-reporting real check errors from an invalid superclass pk. Thanks Clifford Gama for the review. Backport of 7456494 from main.
1 parent 6775888 commit cbdf128

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

django/db/models/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,7 @@ def _check_composite_pk(cls):
17821782
meta = cls._meta
17831783
pk = meta.pk
17841784

1785-
if not isinstance(pk, CompositePrimaryKey):
1785+
if meta.proxy or not isinstance(pk, CompositePrimaryKey):
17861786
return errors
17871787

17881788
seen_columns = defaultdict(list)

docs/releases/5.2.8.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ Bugfixes
1616
* Fixed a bug in Django 5.2 where ``QuerySet.first()`` and ``QuerySet.last()``
1717
raised an error on querysets performing aggregation that selected all fields
1818
of a composite primary key.
19+
20+
* Fixed a bug in Django 5.2 where proxy models having a ``CompositePrimaryKey``
21+
incorrectly raised a ``models.E042`` system check error.

tests/composite_pk/test_checks.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,38 @@ class Bar(Foo):
268268
),
269269
],
270270
)
271+
272+
def test_proxy_model_can_subclass_model_with_composite_pk(self):
273+
class Foo(models.Model):
274+
pk = models.CompositePrimaryKey("a", "b")
275+
a = models.SmallIntegerField()
276+
b = models.SmallIntegerField()
277+
278+
class Bar(Foo):
279+
class Meta:
280+
proxy = True
281+
282+
self.assertEqual(Foo.check(databases=self.databases), [])
283+
self.assertEqual(Bar.check(databases=self.databases), [])
284+
285+
def test_proxy_model_does_not_check_superclass_composite_pk_errors(self):
286+
class Foo(models.Model):
287+
pk = models.CompositePrimaryKey("a", "b")
288+
a = models.SmallIntegerField()
289+
290+
class Bar(Foo):
291+
class Meta:
292+
proxy = True
293+
294+
self.assertEqual(
295+
Foo.check(databases=self.databases),
296+
[
297+
checks.Error(
298+
"'b' cannot be included in the composite primary key.",
299+
hint="'b' is not a valid field.",
300+
obj=Foo,
301+
id="models.E042",
302+
),
303+
],
304+
)
305+
self.assertEqual(Bar.check(databases=self.databases), [])

0 commit comments

Comments
 (0)