Opened 7 years ago

Last modified 7 years ago

#27764 closed Bug

Running migration that deletes child model (in multi-table inheritance) backwards results in an error — at Initial Version

Reported by: Alexandru Mărășteanu Owned by: nobody
Component: Migrations Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

  1. Create two models where one of them inherits the other:
    class Foo(models.Model):
        test = models.CharField(max_length=256)
    
    
    class Bar(Foo):
        pass
    
  1. Generate and run migrations.

migrations/0001_initial.py is:

...
    operations = [
        migrations.CreateModel(
            name='Foo',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('test', models.CharField(max_length=256)),
            ],
        ),
        migrations.CreateModel(
            name='Bar',
            fields=[
                ('foo_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='foo.Foo')),
            ],
            bases=('foo.foo',),
        ),
    ]
...
  1. Delete the child model (Bar)
  1. Generate and run migrations again.

migrations/0002_auto_20170123_1212.py is:

...
        migrations.RemoveField(
            model_name='bar',
            name='foo_ptr',
        ),
        migrations.DeleteModel(
            name='Bar',
        ),
...
  1. Run migrations backwards (i.e. python migrate APP zero)
    django.db.utils.ProgrammingError: column "foo_ptr_id" of relation "foo_bar" already exists
    

Verified with Django (1.9.6, 1.9.12 and 1.10.5) and psycopg2 (2.5.2) on macOS 10.12.2 with Python 2.7.12, PostgreSQL 9.5.4.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top