Opened 6 years ago
Closed 6 years ago
#29819 closed Uncategorized (needsinfo)
ManyToManyField needs "through" argument (and its link table) when working with a legacyDB
Reported by: | Elaci0 | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 2.1 |
Severity: | Normal | Keywords: | ManyToManyField, legacyDB, "through" |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Still working with an existing legacy database integration (I will stop, I promise) and the "--fake-initial" switch
it seems to me that when I use a ManyToManyField inside a model I must include also a link-table using the "through" argument, otherwise Django complies with an "already existing table" error. This happen only with an existing DB, if I start from the scratch no problem at all because all tables are created on-the-fly
(I guess that without any "through" argument the migration process creates such join-table which name doesn't match any of the existing table)
Maybe it's not a problem but I think that perhaps it should be stated into documentation of "inspectDB" ManytoManyField https://docs.djangoproject.com/en/2.1/topics/db/models/#extra-fields-on-many-to-many-relationships"
Change History (5)
follow-up: 2 comment:1 by , 6 years ago
comment:2 by , 6 years ago
Replying to Tim Graham:
I think it's natural to expect that if your table names don't match your models, then Django won't work as expected. Did I miss the point of your suggestion?
It's natural when you know how Django works :-)
I discovered that after some days of debugging the Django's code: I needed to translate my simple many-to-many relationship for my legacyDB, I read the docs and found that ManyToManyField was good and enough but instead my fake-initial migration always failed, until I created that 'junction-table' model which I had to use with the 'through' declaration.
Out of curiosity I tried with the model.py suggested at the ManyToManyField section into the Django's docs (Person/Group/Membership), it worked flawless starting from the scratch.
Later I converted this model to a schema-DB (so I pre-created the tables/columns on a DB) , run the fake-initial migration and it worked again until I removed the 'through' declaration, then complained with the "already existing table" error. Put again the 'through' declaration, it worked. I tried with other working examples on the internet with same results
So going back to your kind replay, I can agree that it could be natural to expect this, but the documentation should warn me that "fake-initial" + ManyToManyField - through doesn't work when a legacyDB is used. Or perhaps something is wrong into the migrations process during the fake-initial ?
follow-up: 4 comment:3 by , 6 years ago
I didn't realize that the many to many relation works except for --fake-initial
. Maybe there's some bug there. Can you debug it to figure out the reason?
comment:4 by , 6 years ago
Replying to Tim Graham:
I didn't realize that the many to many relation works except for
--fake-initial
. Maybe there's some bug there. Can you debug it to figure out the reason?
sure, but I'm quite busy now to try to build my first little site with Django and I'm slow to understand things...
Does this ticket expires or should I open a new one later?
comment:5 by , 6 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
I'll close it as "needsinfo" for now, but you can reopen if you can explain why Django is at fault. Thanks!
I think it's natural to expect that if your table names don't match your models, then Django won't work as expected. Did I miss the point of your suggestion?