Commit a80fb8ae authored by Tim Graham's avatar Tim Graham
Browse files

[1.9.x] Fixed #25922 -- Fixed migrate --fake-initial detection of many-to-many tables.

Backport of fa9ce4e9 from master
parent 571f7b75
Loading
Loading
Loading
Loading
+17 −4
Original line number Diff line number Diff line
@@ -265,6 +265,7 @@ class MigrationExecutor(object):
        apps = after_state.apps
        found_create_model_migration = False
        found_add_field_migration = False
        existing_table_names = self.connection.introspection.table_names(self.connection.cursor())
        # Make sure all create model and add field operations are done
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
@@ -275,7 +276,7 @@ class MigrationExecutor(object):
                    model = global_apps.get_model(model._meta.swapped)
                if model._meta.proxy or not model._meta.managed:
                    continue
                if model._meta.db_table not in self.connection.introspection.table_names(self.connection.cursor()):
                if model._meta.db_table not in existing_table_names:
                    return False, project_state
                found_create_model_migration = True
            elif isinstance(operation, migrations.AddField):
@@ -288,9 +289,21 @@ class MigrationExecutor(object):
                    continue

                table = model._meta.db_table
                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):
                field = model._meta.get_field(operation.name)

                # Handle implicit many-to-many tables created by AddField.
                if field.many_to_many:
                    if field.remote_field.through._meta.db_table not in existing_table_names:
                        return False, project_state
                    else:
                        found_add_field_migration = True
                        continue

                column_names = [
                    column.name for column in
                    self.connection.introspection.get_table_description(self.connection.cursor(), table)
                ]
                if field.column not in column_names:
                    return False, project_state
                found_add_field_migration = True
        # If we get this far and we found at least one CreateModel or AddField migration,
+3 −0
Original line number Diff line number Diff line
@@ -58,3 +58,6 @@ Bugfixes
  behind ``AppRegistryNotReady`` when starting ``runserver`` (:ticket:`25510`).
  This regression appeared in 1.8.5 as a side effect of fixing :ticket:`24704`
  and by mistake the fix wasn't applied to the ``stable/1.9.x`` branch.

* Fixed ``migrate --fake-initial`` detection of many-to-many tables
  (:ticket:`25922`).
+31 −0
Original line number Diff line number Diff line
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Project',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ],
        ),
        migrations.CreateModel(
            name='Task',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ],
        ),
        migrations.AddField(
            model_name='project',
            name='tasks',
            field=models.ManyToManyField(to='Task'),
        ),
    ]
+20 −0
Original line number Diff line number Diff line
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ("migrations", "0001_initial"),
    ]

    operations = [
        migrations.AddField(
            model_name='task',
            name='projects',
            field=models.ManyToManyField(to='Project'),
        ),
    ]
+0 −0

Empty file added.

Loading