Opened 12 years ago

Last modified 3 years ago

#17461 new Cleanup/optimization

Document presumed order of foreign keys on intermediate M2M model

Reported by: flytwokites@… Owned by: nobody
Component: Documentation Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (2)

comment:1 by Gabriel Hurley, 12 years ago

Needs documentation: set
Summary: Need more document for defining a many-to-many relationship from a model to itself and using an intermediary modelDocument presumed order of foreign keys on intermediate M2M model
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

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

comment:3 by Oxylo, 9 years ago

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.

Note: See TracTickets for help on using tickets.
Back to Top