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.
* 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>
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>
* 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>
* status: new => assigned
* owner: nobody => timgraham
--
Ticket URL: <https://code.djangoproject.com/ticket/25904#comment:4>
* 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>