[Django] #36449: Mismatched fields in composite primary key ForeignObject example model

4 views
Skip to first unread message

Django

unread,
Jun 8, 2025, 10:12:47 AMJun 8
to django-...@googlegroups.com
#36449: Mismatched fields in composite primary key ForeignObject example model
-----------------------------------------+-----------------------------
Reporter: Jacob Walls | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Documentation | Version: 5.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-----------------------------------------+-----------------------------
The field types on [https://docs.djangoproject.com/en/5.2/topics
/composite-primary-key/#composite-primary-keys-and-relations this example
model] are not correct; the primary key of `Order` is CharField, and
`Product` is IntegerField:

{{{#!py
class Foo(models.Model):
item_order_id = models.IntegerField()
item_product_id = models.CharField(max_length=20)
item = models.ForeignObject(
OrderLineItem,
on_delete=models.CASCADE,
from_fields=("item_order_id", "item_product_id"),
to_fields=("order_id", "product_id"),
)
}}}

It should be:
{{{#!py
class Foo(models.Model):
item_order_id = models.CharField(max_length=20)
item_product_id = models.IntegerField()
item = models.ForeignObject(
OrderLineItem,
on_delete=models.CASCADE,
from_fields=("item_order_id", "item_product_id"),
to_fields=("order_id", "product_id"),
)
}}}

Found this while trying to write a test with these models, e.g. this
fails:

{{{#!py
In [28]: f = Foo.objects.first()

In [29]: f.item
Out[29]: <OrderLineItem: OrderLineItem object ((1, '1'))>

In [30]: Foo.objects.filter(item=item)
Out[30]:
---------------------------------------------------------------------------
UndefinedFunction Traceback (most recent call
last)
File ~/django/django/db/backends/utils.py:105, in
CursorWrapper._execute(self, sql, params, *ignored_wrapper_args)
104 else:
--> 105 return self.cursor.execute(sql, params)

UndefinedFunction: operator does not exist: character varying = integer
LINE 1: ...."item_order_id", "models_foo"."item_product_id") = ('1', 1)...
^
HINT: No operator matches the given name and argument types. You might
need to add explicit type casts.
}}}


----
While here, also suggest documenting inside the caveats about how
`ForeignObject` differs from `ForeignKey` that the `on_delete` argument is
ignored.
--
Ticket URL: <https://code.djangoproject.com/ticket/36449>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 8, 2025, 10:19:30 AMJun 8
to django-...@googlegroups.com
#36449: Mismatched fields in composite primary key ForeignObject example model
-------------------------------+---------------------------------------
Reporter: Jacob Walls | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Documentation | Version: 5.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Jacob Walls):

* has_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/19540 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36449#comment:1>

Django

unread,
Jun 9, 2025, 4:13:24 AMJun 9
to django-...@googlegroups.com
#36449: Mismatched fields in composite primary key ForeignObject example model
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Documentation | Version: 5.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* stage: Unreviewed => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36449#comment:2>

Django

unread,
Jun 10, 2025, 3:44:50 AMJun 10
to django-...@googlegroups.com
#36449: Mismatched fields in composite primary key ForeignObject example model
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: closed
Component: Documentation | Version: 5.2
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

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

Comment:

In [changeset:"59427547692b433bef3640a96cc0f6601f57532f" 59427547]:
{{{#!CommitTicketReference repository=""
revision="59427547692b433bef3640a96cc0f6601f57532f"
Fixed #36449 -- Fixed field types in example model using ForeignObject.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36449#comment:3>

Django

unread,
Jun 10, 2025, 3:49:06 AMJun 10
to django-...@googlegroups.com
#36449: Mismatched fields in composite primary key ForeignObject example model
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: closed
Component: Documentation | Version: 5.2
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"6f99c8856d17e29564bc5d432bb7d7b8f73bc73c" 6f99c88]:
{{{#!CommitTicketReference repository=""
revision="6f99c8856d17e29564bc5d432bb7d7b8f73bc73c"
[5.2.x] Fixed #36449 -- Fixed field types in example model using
ForeignObject.

Backport of 59427547692b433bef3640a96cc0f6601f57532f from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36449#comment:4>
Reply all
Reply to author
Forward
0 new messages