﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27885	Migration creates index of a deleted table	ihucos	nobody	"


If we create a model in a migration with an indexed field, then delete that model, migrations will still try to create the index on a nonexistent table.

That scenario occurred after squashing migrations.

Happened on Django 1.8.13

Example Migration that reproduces this:


{{{
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('bookoya', '0085_auto_20170214_1425'),
    ]

    operations = [
        migrations.CreateModel(
            name='MyModel',
            fields=[
                ('name', models.CharField(max_length=100, unique=True, serialize=False, primary_key=True, db_index=True)),
            ],
        ),

        migrations.DeleteModel(
            name='MyModel',
        ),
    ]
}}}





{{{
~/R $ python manage.py migrate
Waiting for database...
(0.000) select 1; args=None
Database ready
(0.003) SELECT 1 FROM pg_database WHERE datname = 'template_postgis' LIMIT 1;; args=('template_postgis',)
(0.001) CREATE EXTENSION IF NOT EXISTS postgis; args=None
(0.009)
            SELECT c.relname, c.relkind
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind IN ('r', 'v')
                AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
                AND pg_catalog.pg_table_is_visible(c.oid); args=None
(0.005) SELECT ""django_migrations"".""app"", ""django_migrations"".""name"" FROM ""django_migrations""; args=()
Operations to perform:
  Synchronize unmigrated apps: checkrequirements, s3_folder_storage, tastypie_oauth, gis, postgres, cacheops, webpack_loader, staticfiles, debug_toolbar, djorm_core, waitfordb, messages, django_otp, django_nose, storages, session_cleanup,
 djorm_hstore, djorm_expressions
  Apply all migrations: oauth2_provider, qmore, admin, oauth_provider, sendgrid, sessions, tastypie, otp_static, flatpages, sites, kombu_transport_django, invoices, contenttypes, bookoya, auth, recommend, otp_totp, two_factor
Synchronizing apps without migrations:
(0.005)
            SELECT c.relname, c.relkind
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind IN ('r', 'v')
                AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
                AND pg_catalog.pg_table_is_visible(c.oid); args=None
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying bookoya.0086_auto_20170227_1359...CREATE TABLE ""bookoya_mymodel"" (""name"" varchar(100) NOT NULL PRIMARY KEY); (params None)
(0.025) CREATE TABLE ""bookoya_mymodel"" (""name"" varchar(100) NOT NULL PRIMARY KEY); args=None
DROP TABLE ""bookoya_mymodel"" CASCADE; (params [])
(0.002) DROP TABLE ""bookoya_mymodel"" CASCADE; args=[]
DELETE FROM geometry_columns WHERE f_table_name = 'bookoya_mymodel'; (params [])
(0.002) DELETE FROM geometry_columns WHERE f_table_name = 'bookoya_mymodel'; args=[]
CREATE INDEX ""bookoya_mymodel_name_e935283cc44e389_like"" ON ""bookoya_mymodel"" (""name"" varchar_pattern_ops); (params [])
(0.012) CREATE INDEX ""bookoya_mymodel_name_e935283cc44e389_like"" ON ""bookoya_mymodel"" (""name"" varchar_pattern_ops); args=[]
Traceback (most recent call last):
  File ""manage.py"", line 47, in <module>
    execute_from_command_line(sys.argv)
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py"", line 354, in execute_from_command_line
    utility.execute()
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py"", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/base.py"", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/base.py"", line 445, in execute
    output = self.handle(*args, **options)
  File ""/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py"", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py"", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py"", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py"", line 91, in __exit__
    self.execute(sql)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py"", line 111, in execute
    cursor.execute(sql, params)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py"", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File ""/usr/local/lib/python2.7/dist-packages/cacheops/transaction.py"", line 86, in execute
    result = self._no_monkey.execute(self, sql, params)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py"", line 64, in execute
    return self.cursor.execute(sql, params)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/utils.py"", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File ""/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py"", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation ""bookoya_mymodel"" does not exist
}}}


"	Bug	closed	Migrations	1.8	Normal	duplicate			Unreviewed	0	0	0	0	0	0
