[Django] #25904: --fake-initial does not work for ManyToMany with through Model

30 views
Skip to first unread message

Django

unread,
Dec 9, 2015, 1:35:37 PM12/9/15
to django-...@googlegroups.com
#25904: --fake-initial does not work for ManyToMany with through Model
--------------------------------+--------------------
Reporter: maciej-pawlisz | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------
I came across this issue when migrating from django 1.6.11 and south. When
model has ManyToMany field then automatic initial migration contains
`AddField` operation for this field, which of course is not present in
database, so django thinks that it cannot fake this migration.

How to reproduce:
`fake_through.models.py`:
{{{
from django.db import models
class A(models.Model):
b = models.ManyToManyField('B', through="C")

class B(models.Model):
pass

class C(models.Model):
a = models.ForeignKey('A')
b = models.ForeignKey('B')
}}}
commands:
{{{
python manage.py makemigrations fake_through
python manage.py migrate fake_through
python manage.py migrate fake_through zero --fake
python manage.py migrate fake_through --fake-initial
django.db.utils.OperationalError: table "fake_through_a" already exists
}}}

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

Django

unread,
Dec 10, 2015, 11:04:16 AM12/10/15
to django-...@googlegroups.com
#25904: --fake-initial does not work for ManyToMany with through Model
--------------------------------+--------------------------------------

Reporter: maciej-pawlisz | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Did you set the version on the ticket correctly? I might expect that
behavior in Django 1.8 but if you generated the migrations with Django
1.9, then they should have the
[https://docs.djangoproject.com/en/1.9/topics/migrations/#initial-
migrations initial] attribute set such that this shouldn't happen.

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

Django

unread,
Dec 10, 2015, 11:30:09 AM12/10/15
to django-...@googlegroups.com
#25904: --fake-initial does not work for ManyToMany with through Model
--------------------------------+--------------------------------------

Reporter: maciej-pawlisz | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

Comment (by maciej-pawlisz):

It was Django 1.9. From what I see when `initial` is `True`, django
proceeds to checking db schema and this check fails. Actually it fails
with every `ManyToManyField`, because it is added in `AddField` operation,
but is not present in database schema:
djang.db.migrations.executor.py:291
{{{
db_field = model._meta.get_field(operation.name).column
fields =
self.connection.introspection.get_table_description(self.connection.cursor(),
table)
if db_field not in (f.name for f in fields):
return False, project_state
}}}
Easy fix would be adding this line before that check:
{{{
if isinstance(operation.field,models.ManyToMany):
continue

}}}
But maybe the bug is somewhere else ie. why
`model._meta.get_field(operation.name).column` is not None for
ManyToManyField ?

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

Django

unread,
Dec 10, 2015, 4:43:49 PM12/10/15
to django-...@googlegroups.com
#25904: --fake-initial does not work for ManyToMany with through Model
---------------------------------+------------------------------------

Reporter: maciej-pawlisz | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


Comment:

Thanks, I understand now. I'll have to look at the code a bit more to
advise on the proper fix. For reference,
db97a8849519a3933bf4abd2184efd68ebc21965 added the `Migration.initial`
option.

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

Django

unread,
Dec 19, 2015, 11:29:08 AM12/19/15
to django-...@googlegroups.com
#25904: --fake-initial does not work for ManyToMany with through Model
---------------------------------+-------------------------------------
Reporter: maciej-pawlisz | Owner: timgraham
Type: Bug | Status: assigned
Component: Migrations | Version: 1.9

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

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

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


--
Ticket URL: <https://code.djangoproject.com/ticket/25904#comment:4>

Django

unread,
Dec 19, 2015, 11:38:58 AM12/19/15
to django-...@googlegroups.com
#25904: --fake-initial does not work for ManyToMany with through Model
---------------------------------+-------------------------------------
Reporter: maciej-pawlisz | Owner: timgraham
Type: Bug | Status: closed
Component: Migrations | Version: 1.9
Severity: Release blocker | Resolution: duplicate

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

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


Comment:

The fix for #25904 should address this as whether or not the
`ManyToManyField` uses an explicit `through` model doesn't matter.

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

Reply all
Reply to author
Forward
0 new messages