Many to Many fields and through table

34 views
Skip to first unread message

Mark Phillips

unread,
Dec 19, 2017, 9:54:08 PM12/19/17
to django users
Is there any downside to creating a through table in a many to many relationship when there isn't any "extra" related data, but there might be in the future? 

I have read that migrating from a simple m2m relationship to a m2m with a through table takes some jumping through hurdles, so I am considering creating the through table now in case "the powers that be" (ie end users!) decide we need some extra data for the relationship in the future.

Thanks!

Mark

Simon Charette

unread,
Dec 19, 2017, 10:10:26 PM12/19/17
to Django users
Hello Mark,

The only downside I can think of is that you won't be able to create
relationships from the m2m managers, you'll have to create the
relationship directly instead.

This isn't such a big issue if you plan on adding fields later to
the intermediary model later on anyway.

You can read more about the subject in the documentation[0]

Best,
Simon

[0] https://docs.djangoproject.com/en/2.0/topics/db/models/#intermediary-manytomany

James Schneider

unread,
Dec 19, 2017, 10:10:58 PM12/19/17
to django...@googlegroups.com
I don't believe there is any downside other than the extra few lines required for the intermediary model definition. Functionally it is the same AFAIK.

-James

johnasm...@gmail.com

unread,
Jul 4, 2018, 4:38:34 PM7/4/18
to Django users
Hi Mark,

Thank you for your question, I might actually do the same. I just had issues on a model with 2 M2M relationships (one using an intermediary table, one without). 

I would just caveat what James Schneider said, there are some differences. My simply M2M relationship threw me Field Required Errors while the one using a intermediary table didn't, implicitly making the field required=False.

It seems like the default M2M is required as expected[1], but it's tricky to have the M2M through relationship not throw the same error.  

Best,
John

Note: I noticed this through DRF, haven't tested on a small reproducible case in pure Django.
Reply all
Reply to author
Forward
0 new messages