#23288 closed Bug (fixed)
Unable to correctly access data in table during migration
Reported by: | Matthew Meyer | Owned by: | Andrew Godwin |
---|---|---|---|
Component: | Migrations | Version: | 1.7-rc-2 |
Severity: | Release blocker | Keywords: | |
Cc: | info@…, cmawebsite@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I created a profile and then setup some relations to it. When I change the relation from the profile to the User model and try to access the data after the models.py file has been changed but before the migration has been completed the members field returns only a single profile objects instead of the ones that were assigned. Dumping the data shows that users with id 2,3,4,5 are in team with id 1, but when accessing the field from the migration only user id 2 is returned.
Attachments (1)
Change History (12)
by , 10 years ago
Attachment: | migration_example.tgz added |
---|
comment:1 by , 10 years ago
Severity: | Normal → Release blocker |
---|---|
Triage Stage: | Unreviewed → Accepted |
Yikes! I regret to say I can reproduce this. And it happens even without the Profile-User change to your models.py.
comment:2 by , 10 years ago
I'm out of time, but I put the example on github if someone else wants to give it a try.
compare running the this code by hand in the shell vs in the migration
comment:3 by , 10 years ago
Cc: | added |
---|---|
Needs tests: | set |
I can confirm the issue too. The repository holds a simplified test case: https://github.com/collinanderson/ticket23288
comment:4 by , 10 years ago
Cc: | added |
---|
The issue happens when querying a many-to-many. The ORM is incorrectly using the ForeignKey relationship.
A deep link to the test case: https://github.com/collinanderson/ticket23288/commit/21a501c963f8a2c50278d438ef1b87cc7cd3031d#diff-186cb93781c2f86fd8d75aac5cd19c2dR6
comment:6 by , 10 years ago
Well that was nasty. The root cause was that deconstruct()
wasn't including related_name
, and so both the FK and the M2M ended up with the same related_name - and without model validation running against the migration models (as they're supposedly already validated if deconstruct is accurate) Django didn't catch this and so the filter it passed to the query backend was team__id = X
, and Django picked the first thing it knew about with a related_name
of "team"
- the ForeignKey, not the ManyToMany.
The fix makes sure deconstruct also knows about related_name and a bunch of other similar fields that were missing before (most notably, I have left out limit_choices_to
for now as it has no database or model layer effect and returns a set of mostly unserialisable things).
If you have existing migrations from before the fix, you'll either need to regenerate them or manually add related_name
into the field definitions yourself (doing this makes the supplied test case pass for me).
comment:7 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:8 by , 10 years ago
Has patch: | set |
---|---|
Needs tests: | unset |
Resolution: | fixed |
Status: | closed → new |
Triage Stage: | Accepted → Ready for checkin |
Could use a [1.7.x] backport.
comment:9 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
sorry nevermind. it's right here, the trac hook must have not worked: https://github.com/django/django/commit/b5784048e085e8db5d0e959c12278ef727994e98
Example application that recreates the problem