Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#27764 closed Bug

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

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 (last modified by Alexandru Mărășteanu)

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

migrations/ is:

    operations = [
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('test', models.CharField(max_length=256)),
                ('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')),
  1. Delete the child model (Bar)
  1. Generate and run migrations again.

migrations/ is:

  1. Run migrations backwards
    $ python migrate foo zero
    Operations to perform:
      Unapply all migrations: foo
    Running migrations:
      Rendering model states... DONE
      Unapplying foo.0002_auto_20170123_1212...Traceback (most recent call last):
      File "", line 10, in <module>
      File "/djbug/lib/python2.7/site-packages/django/core/management/", line 367, in execute_from_command_line
      File "/djbug/lib/python2.7/site-packages/django/core/management/", line 359, in execute
      File "/djbug/lib/python2.7/site-packages/django/core/management/", line 294, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/djbug/lib/python2.7/site-packages/django/core/management/", line 345, in execute
        output = self.handle(*args, **options)
      File "/djbug/lib/python2.7/site-packages/django/core/management/commands/", line 204, in handle
      File "/djbug/lib/python2.7/site-packages/django/db/migrations/", line 119, in migrate
        state = self._migrate_all_backwards(plan, full_plan, fake=fake)
      File "/djbug/lib/python2.7/site-packages/django/db/migrations/", line 194, in _migrate_all_backwards
        self.unapply_migration(states[migration], migration, fake=fake)
      File "/djbug/lib/python2.7/site-packages/django/db/migrations/", line 264, in unapply_migration
        state = migration.unapply(state, schema_editor)
      File "/djbug/lib/python2.7/site-packages/django/db/migrations/", line 178, in unapply
        operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
      File "/djbug/lib/python2.7/site-packages/django/db/migrations/operations/", line 153, in database_backwards
        schema_editor.add_field(from_model, to_model._meta.get_field(
      File "/djbug/lib/python2.7/site-packages/django/db/backends/postgresql/", line 21, in add_field
        super(DatabaseSchemaEditor, self).add_field(model, field)
      File "/djbug/lib/python2.7/site-packages/django/db/backends/base/", line 409, in add_field
        self.execute(sql, params)
      File "/djbug/lib/python2.7/site-packages/django/db/backends/base/", line 112, in execute
        cursor.execute(sql, params)
      File "/djbug/lib/python2.7/site-packages/django/db/backends/", line 79, in execute
        return super(CursorDebugWrapper, self).execute(sql, params)
      File "/djbug/lib/python2.7/site-packages/django/db/backends/", line 64, in execute
        return self.cursor.execute(sql, params)
      File "/djbug/lib/python2.7/site-packages/django/db/", line 94, in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
      File "/djbug/lib/python2.7/site-packages/django/db/backends/", line 64, in execute
        return self.cursor.execute(sql, params)
    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 (3)

comment:1 Changed 6 years ago by Alexandru Mărășteanu

Description: modified (diff)

comment:2 Changed 6 years ago by Alexandru Mărășteanu

Description: modified (diff)

comment:3 Changed 6 years ago by Tim Graham

Resolution: duplicate
Status: newclosed

I think this is a duplicate of #24424 (presumably that ticket will also verify that the backwards operations also work). If the RemoveField operation is omitted by the autodetector, then this seems to work.

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