﻿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
24061	When deleting a model with foreignkey, redundant RemoveField created.	Aur Saraf	Andriy Sokolovskiy	"{{{
$ pip freeze
Django==1.7.1
wsgiref==0.1.2
}}}

I add a model that inherits from another model, makemigrations, delete that model, makemigrations, and the SQLite backend generates this illegal SQL when trying to delete the last field ""model_ptr"" from the table:

{{{
INSERT INTO ""bug_b__new"" () SELECT  FROM ""bug_b""
}}}

Easy repro:

{{{
$ mkvirtualenv bug
$ git clone git@github.com:SonOfLilit/django-sqlite-migrations-bug.git
$ cd django-sqlite-migrations-bug
$ python manage.py test
}}}

Here is what happens: https://dpaste.de/LYqP


----

'''RELATED ISSUE:''

Consider following:

{{{

class ModelA(models.Model):
    field1 = models.BooleanField(default=False)

class ModelB(models.Model):
    another_field1 = models.TextField(blank=True)
    related_field = models.ForeignKey(ModelA)
}}}

After creating inital migration, I deleted model, so migration will look like:

{{{

class Migration(migrations.Migration):

    dependencies = [
        ('blankapp', '0001_initial'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='modelb',
            name='related_field',
        ),
        migrations.DeleteModel(
            name='ModelB',
        ),
    ]
}}}

And, when you will try to migrate backwards, LookupError: App 'blankapp' doesn't have a 'modelb' model. will be raised, because migration will try to add related_field to model which does not exists yet. "	Bug	assigned	Migrations	master	Normal				Accepted	0	0	0	0	0	0
