﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36704	Proxy model with a CompositePrimaryKey fails system check with false positive: (models.E042) '<field>' cannot be included in the composite primary key	Hal Blackburn	Hal Blackburn	"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."	Bug	closed	Core (System checks)	5.2	Release blocker	fixed	compositeprimarykey	Hal Blackburn	Ready for checkin	1	0	0	0	0	0
