[Django] #17461: Need more document for defining a many-to-many relationship from a model to itself and using an intermediary model

24 views
Skip to first unread message

Django

unread,
Dec 25, 2011, 7:36:12 AM12/25/11
to django-...@googlegroups.com
#17461: Need more document for defining a many-to-many relationship from a model to
itself and using an intermediary model
-------------------------------+--------------------
Reporter: flytwokites@… | Owner: nobody
Type: Uncategorized | Status: new
Component: Documentation | Version: 1.3
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
When defining a many-to-many relationship from a model to itself and using
an intermediary model,

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

followers = models.ManyToManyField('self', through='Relationship',
symmetrical=False)

def __unicode__(self):
return self.name

class Relationship(models.Model):
target = models.ForeignKey('User', related_name='r1')
follower = models.ForeignKey('User', related_name='r2')
created_at = models.DateTimeField(auto_now_add=True)
}}}

It seems that django determine 'from field' and 'to field' by it's
definition order,
the first field always used as 'from field' and the second field always
used as 'to field'.
So I MUST put `target` field defintion above the `follower` field.

I checked the document but can not found any infomation to confirm it,
I think the document should clearly explained the rule.

--
Ticket URL: <https://code.djangoproject.com/ticket/17461>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 26, 2011, 12:15:10 AM12/26/11
to django-...@googlegroups.com
#17461: Document presumed order of foreign keys on intermediate M2M model
--------------------------------------+------------------------------------
Reporter: flytwokites@… | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.3
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by gabrielhurley):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 1
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

This is true specifically for asymmetric M2M's using an intermediate
model; it's an implementation detail coded into the accessor calls here:

https://code.djangoproject.com/browser/django/trunk/django/db/models/fields/related.py#L1135

and a little lower down, here:

https://code.djangoproject.com/browser/django/trunk/django/db/models/fields/related.py#L1154

It's worth noting that this the case in the docs, probably under the
"symmetrical" section of the docs here:

https://docs.djangoproject.com/en/1.3/ref/models/fields/#django.db.models.ManyToManyField.symmetrical

--
Ticket URL: <https://code.djangoproject.com/ticket/17461#comment:1>

Django

unread,
Mar 7, 2015, 9:17:53 AM3/7/15
to django-...@googlegroups.com
#17461: Document presumed order of foreign keys on intermediate M2M model
--------------------------------------+------------------------------------
Reporter: flytwokites@… | Owner: Oxylo
Type: Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.3

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Oxylo):

* owner: nobody => Oxylo
* status: new => assigned


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

Django

unread,
Mar 7, 2015, 2:25:15 PM3/7/15
to django-...@googlegroups.com
#17461: Document presumed order of foreign keys on intermediate M2M model
--------------------------------------+------------------------------------
Reporter: flytwokites@… | Owner: Oxylo
Type: Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.3

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Oxylo):

Confirmed today on master that Django still determines the 'from field'
and the 'to field' by its definition order in this case. We proved this by
comparing the SQL generated for both orderings of the fields "target" and
"follower"

When the field "target" is above the field "follower"

u1 = User.objects.create(name="Erik")
print u1.followers.all().query

returns the desired SQL:

SELECT "testm2m_user"."id", "testm2m_user"."name"
FROM "testm2m_user" INNER JOIN "testm2m_relationship"
ON ( "testm2m_user"."id" = "testm2m_relationship"."follower_id" )
WHERE "testm2m_relationship"."target_id" = 1

but if the field "follower" is above the field "target" the following
(incorrect) SQL is returned:

SELECT "testm2m_user"."id", "testm2m_user"."name"
FROM "testm2m_user" INNER JOIN "testm2m_relationship"
ON ( "testm2m_user"."id" = "testm2m_relationship"."target_id" )
WHERE "testm2m_relationship"."follower_id" = 1

Conclusion:
This issue still persists and needs to be documented accordingly.

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

Django

unread,
Nov 4, 2024, 3:48:10 AM11/4/24
to django-...@googlegroups.com
#17461: Document presumed order of foreign keys on intermediate M2M model
-------------------------------------+-------------------------------------
Reporter: flytwokites@… | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.3
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* has_patch: 0 => 1
* needs_docs: 1 => 0
* owner: nobody => Clifford Gama
* status: new => assigned

Comment:

[https://github.com/django/django/pull/18762 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/17461#comment:4>

Django

unread,
Mar 26, 2025, 5:25:48 AM3/26/25
to django-...@googlegroups.com
#17461: Document the presumed order of foreign keys on the intermediary model of a
self-referential m2m.
-------------------------------------+-------------------------------------
Reporter: flytwokites@… | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.3
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* summary: Document presumed order of foreign keys on intermediate M2M
model =>
Document the presumed order of foreign keys on the intermediary model
of a self-referential m2m.

--
Ticket URL: <https://code.djangoproject.com/ticket/17461#comment:5>

Django

unread,
Apr 26, 2025, 9:00:24 AM4/26/25
to django-...@googlegroups.com
#17461: Document the presumed order of foreign keys on the intermediary model of a
self-referential m2m.
-------------------------------------+-------------------------------------
Reporter: flytwokites@… | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.3
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Giannis Terzopoulos):

* needs_better_patch: 0 => 1

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

Django

unread,
Apr 26, 2025, 10:03:51 AM4/26/25
to django-...@googlegroups.com
#17461: Document the presumed order of foreign keys on the intermediary model of a
self-referential m2m.
-------------------------------------+-------------------------------------
Reporter: flytwokites@… | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.3
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/17461#comment:7>

Django

unread,
Apr 26, 2025, 10:40:37 AM4/26/25
to django-...@googlegroups.com
#17461: Document the presumed order of foreign keys on the intermediary model of a
self-referential m2m.
-------------------------------------+-------------------------------------
Reporter: flytwokites@… | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.3
Severity: Normal | Resolution:
Keywords: | 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 Giannis Terzopoulos):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/17461#comment:8>

Django

unread,
May 2, 2025, 2:57:06 AM5/2/25
to django-...@googlegroups.com
#17461: Document the presumed order of foreign keys on the intermediary model of a
self-referential m2m.
-------------------------------------+-------------------------------------
Reporter: flytwokites@… | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.3
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: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

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

Comment:

In [changeset:"9d93e35c207a001de1aa9ca9165bdec824da9021" 9d93e35c]:
{{{#!CommitTicketReference repository=""
revision="9d93e35c207a001de1aa9ca9165bdec824da9021"
Fixed #17461 -- Doc'd the presumed order of foreign keys on the
intermediary model of a self-referential m2m.

Thanks Giannis Terzopoulos and Sarah Boyce for the reviews.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/17461#comment:9>

Django

unread,
May 2, 2025, 3:02:51 AM5/2/25
to django-...@googlegroups.com
#17461: Document the presumed order of foreign keys on the intermediary model of a
self-referential m2m.
-------------------------------------+-------------------------------------
Reporter: flytwokites@… | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.3
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: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"3c887e5e50ddcb84adef269e9558e603ca0ff620" 3c887e5e]:
{{{#!CommitTicketReference repository=""
revision="3c887e5e50ddcb84adef269e9558e603ca0ff620"
[5.2.x] Fixed #17461 -- Doc'd the presumed order of foreign keys on the
intermediary model of a self-referential m2m.

Thanks Giannis Terzopoulos and Sarah Boyce for the reviews.

Backport of 9d93e35c207a001de1aa9ca9165bdec824da9021 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/17461#comment:10>
Reply all
Reply to author
Forward
0 new messages