[Django] #36704: Proxy model with a CompositePrimaryKey fails system check with false positive: (models.E042) '<field>' cannot be included in the composite primary key

9 views
Skip to first unread message

Django

unread,
Nov 3, 2025, 3:17:20 AM11/3/25
to django-...@googlegroups.com
#36704: Proxy model with a CompositePrimaryKey fails system check with false
positive: (models.E042) '<field>' cannot be included in the composite
primary key
-------------------------------------+-------------------------------------
Reporter: Hal Blackburn | Type: Bug
Status: new | Component: Core
| (System checks)
Version: 5.2 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
A proxy subclass model of a parent model that uses CompositePrimaryKey
causes `manage.py check` to fail:

{{{
$ python manage.py check
SystemCheckError: System check identified some issues:

ERRORS:
example.ProxyOrderLineItem: (models.E042) 'order_id' cannot be included in
the composite primary key.
HINT: 'order_id' field is not a local field.
example.ProxyOrderLineItem: (models.E042) 'product_id' cannot be included
in the composite primary key.
HINT: 'product_id' field is not a local field.

System check identified 2 issues (0 silenced).
}}}

This `models.py` module will reproduce the issue:
{{{
# Example from https://docs.djangoproject.com/en/5.2/topics/composite-
primary-key/
from django.db import models


class Product(models.Model):
name = models.CharField(max_length=100)


class Order(models.Model):
reference = models.CharField(max_length=20, primary_key=True)


class OrderLineItem(models.Model):
pk = models.CompositePrimaryKey("product_id", "order_id")
product = models.ForeignKey(Product, on_delete=models.CASCADE)
order = models.ForeignKey(Order, on_delete=models.CASCADE)
quantity = models.IntegerField()


# This proxy model triggers the check error
class ProxyOrderLineItem(OrderLineItem):
class Meta:
proxy = True
}}}

The issue is caused by
[https://github.com/django/django/blob/05ba1a9228128614fb3c475f1c4bdf0160f44dba/django/db/models/base.py#L1825-L1826
these two lines of the checks for classes with CompositePrimaryKey]:

{{{
elif field not in meta.local_fields:
hint = f"{field_name!r} field is not a local field."
}}}

The `meta.local_fields` is empty for proxy subclasses, but the check
passes for the parent class because the `meta.local_fields` contains the
fields in the parent.

I believe this is a false-positive, as the proxy model seems to work as
expected if the failed checks are suppressed.
--
Ticket URL: <https://code.djangoproject.com/ticket/36704>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 3, 2025, 4:23:40 AM11/3/25
to django-...@googlegroups.com
#36704: Proxy model with a CompositePrimaryKey fails system check with false
positive: (models.E042) '<field>' cannot be included in the composite
primary key
--------------------------------------+------------------------------------
Reporter: Hal Blackburn | Owner: (none)
Type: Bug | Status: new
Component: Core (System checks) | Version: 5.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Clifford Gama):

* has_patch: 1 => 0
* stage: Unreviewed => Accepted

Comment:

Thanks for the report!

Would you like to prepare a patch?

Since `_check_composite_pk()`
[https://github.com/django/django/blob/05ba1a9228128614fb3c475f1c4bdf0160f44dba/django/db/models/base.py#L1799
has no proxy-model specific checks], we may also choose to skip the checks
for proxy models. This would fix this issue and prevent duplicate errors
reports when there are errors.
--
Ticket URL: <https://code.djangoproject.com/ticket/36704#comment:1>

Django

unread,
Nov 3, 2025, 5:59:23 AM11/3/25
to django-...@googlegroups.com
#36704: Proxy model with a CompositePrimaryKey fails system check with false
positive: (models.E042) '<field>' cannot be included in the composite
primary key
--------------------------------------+------------------------------------
Reporter: Hal Blackburn | Owner: (none)
Type: Bug | Status: new
Component: Core (System checks) | Version: 5.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by Hal Blackburn):

You're welcome! Sure, I'd be happy to make a patch.

Good idea to skip these checks for proxy models. Shall we return early
from `_check_composite_pk()`
[https://github.com/django/django/blob/05ba1a9228128614fb3c475f1c4bdf0160f44dba/django/db/models/base.py#L1804-L1805
at this point] that `if not isinstance(pk, CompositePrimaryKey):` also
returns early?
--
Ticket URL: <https://code.djangoproject.com/ticket/36704#comment:2>

Django

unread,
Nov 3, 2025, 6:01:29 AM11/3/25
to django-...@googlegroups.com
#36704: Proxy model with a CompositePrimaryKey fails system check with false
positive: (models.E042) '<field>' cannot be included in the composite
primary key
-------------------------------------+-------------------------------------
Reporter: Hal Blackburn | Owner: Hal
| Blackburn
Type: Bug | Status: assigned
Component: Core (System | Version: 5.2
checks) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hal Blackburn):

* owner: (none) => Hal Blackburn
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36704#comment:3>

Django

unread,
Nov 3, 2025, 8:41:49 AM11/3/25
to django-...@googlegroups.com
#36704: Proxy model with a CompositePrimaryKey fails system check with false
positive: (models.E042) '<field>' cannot be included in the composite
primary key
-------------------------------------+-------------------------------------
Reporter: Hal Blackburn | Owner: Hal
| Blackburn
Type: Bug | Status: assigned
Component: Core (System | Version: 5.2
checks) |
Severity: Release blocker | Resolution:
Keywords: compositeprimarykey | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* keywords: => compositeprimarykey
* severity: Normal => Release blocker

--
Ticket URL: <https://code.djangoproject.com/ticket/36704#comment:4>

Django

unread,
Nov 4, 2025, 3:44:53 AM11/4/25
to django-...@googlegroups.com
#36704: Proxy model with a CompositePrimaryKey fails system check with false
positive: (models.E042) '<field>' cannot be included in the composite
primary key
-------------------------------------+-------------------------------------
Reporter: Hal Blackburn | Owner: Hal
| Blackburn
Type: Bug | Status: assigned
Component: Core (System | Version: 5.2
checks) |
Severity: Release blocker | Resolution:
Keywords: compositeprimarykey | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hal Blackburn):

* has_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/20053 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36704#comment:5>

Django

unread,
Nov 4, 2025, 8:52:46 AM11/4/25
to django-...@googlegroups.com
#36704: Proxy model with a CompositePrimaryKey fails system check with false
positive: (models.E042) '<field>' cannot be included in the composite
primary key
-------------------------------------+-------------------------------------
Reporter: Hal Blackburn | Owner: Hal
| Blackburn
Type: Bug | Status: assigned
Component: Core (System | Version: 5.2
checks) |
Severity: Release blocker | Resolution:
Keywords: compositeprimarykey | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36704#comment:6>

Django

unread,
Nov 4, 2025, 11:59:33 AM11/4/25
to django-...@googlegroups.com
#36704: Proxy model with a CompositePrimaryKey fails system check with false
positive: (models.E042) '<field>' cannot be included in the composite
primary key
-------------------------------------+-------------------------------------
Reporter: Hal Blackburn | Owner: Hal
| Blackburn
Type: Bug | Status: closed
Component: Core (System | Version: 5.2
checks) |
Severity: Release blocker | Resolution: fixed
Keywords: compositeprimarykey | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls <jacobtylerwalls@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"74564946c3b42a2ef7d087047e49873847a7e1d9" 74564946]:
{{{#!CommitTicketReference repository=""
revision="74564946c3b42a2ef7d087047e49873847a7e1d9"
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.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36704#comment:7>

Django

unread,
Nov 4, 2025, 12:00:19 PM11/4/25
to django-...@googlegroups.com
#36704: Proxy model with a CompositePrimaryKey fails system check with false
positive: (models.E042) '<field>' cannot be included in the composite
primary key
-------------------------------------+-------------------------------------
Reporter: Hal Blackburn | Owner: Hal
| Blackburn
Type: Bug | Status: closed
Component: Core (System | Version: 5.2
checks) |
Severity: Release blocker | Resolution: fixed
Keywords: compositeprimarykey | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jacob Walls <jacobtylerwalls@…>):

In [changeset:"12042b8978a686d815636b11b9ea602b7031ae0a" 12042b8]:
{{{#!CommitTicketReference repository=""
revision="12042b8978a686d815636b11b9ea602b7031ae0a"
[6.0.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 74564946c3b42a2ef7d087047e49873847a7e1d9 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36704#comment:8>

Django

unread,
Nov 4, 2025, 12:01:20 PM11/4/25
to django-...@googlegroups.com
#36704: Proxy model with a CompositePrimaryKey fails system check with false
positive: (models.E042) '<field>' cannot be included in the composite
primary key
-------------------------------------+-------------------------------------
Reporter: Hal Blackburn | Owner: Hal
| Blackburn
Type: Bug | Status: closed
Component: Core (System | Version: 5.2
checks) |
Severity: Release blocker | Resolution: fixed
Keywords: compositeprimarykey | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jacob Walls <jacobtylerwalls@…>):

In [changeset:"cbdf128cb316bccf9ca3b3b4966e57bd050bfc8a" cbdf128]:
{{{#!CommitTicketReference repository=""
revision="cbdf128cb316bccf9ca3b3b4966e57bd050bfc8a"
[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 74564946c3b42a2ef7d087047e49873847a7e1d9 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36704#comment:9>
Reply all
Reply to author
Forward
0 new messages