#27765: Migration to delete child model (multi-table inheritance) results in an
error when using SQLite
-------------------------------------+-------------------------------------
     Reporter:  Alexandru            |                    Owner:  nobody
  Mărășteanu                         |
         Type:  Bug                  |                   Status:  new
    Component:  Migrations           |                  Version:  1.10
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Alexandru Mărășteanu:

Old 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
> }}}
>
> 2. 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',),
>     ),
> ]
> ...
> }}}
>
> 3. Delete the child model (`Bar`)
>
> 4. Generate migrations
> `migrations/0002_auto_20170123_1212.py` is:
> {{{
> ...
> operations = [
>     migrations.RemoveField(
>         model_name='bar',
>         name='foo_ptr',
>     ),
>     migrations.DeleteModel(
>         name='Bar',
>     ),
> ]
> ...
> }}}
>
> 5. Run migrations
> {{{
> $ python manage.py migrate
> Operations to perform:
>   Apply all migrations: admin, contenttypes, foo, auth, sessions
> Running migrations:
>   Rendering model states... DONE
>   Applying foo.0002_auto_20170123_1212...Traceback (most recent call
> last):
>   File "manage.py", line 10, in <module>
>     execute_from_command_line(sys.argv)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/core/management/__init__.py", line 353, in
> execute_from_command_line
>     utility.execute()
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/core/management/__init__.py", line 345, in execute
>     self.fetch_command(subcommand).run_from_argv(self.argv)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/core/management/base.py", line 348, in run_from_argv
>     self.execute(*args, **cmd_options)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/core/management/base.py", line 399, in execute
>     output = self.handle(*args, **options)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/core/management/commands/migrate.py", line 200, in handle
>     executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/migrations/executor.py", line 92, in migrate
>     self._migrate_all_forwards(plan, full_plan, fake=fake,
> fake_initial=fake_initial)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/migrations/executor.py", line 121, in
> _migrate_all_forwards
>     state = self.apply_migration(state, migration, fake=fake,
> fake_initial=fake_initial)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/migrations/executor.py", line 198, in apply_migration
>     state = migration.apply(state, schema_editor)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/migrations/migration.py", line 123, in apply
>     operation.database_forwards(self.app_label, schema_editor, old_state,
> project_state)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/migrations/operations/fields.py", line 121, in
> database_forwards
>     schema_editor.remove_field(from_model,
> from_model._meta.get_field(self.name))
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/backends/sqlite3/schema.py", line 247, in remove_field
>     self._remake_table(model, delete_fields=[field])
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/backends/sqlite3/schema.py", line 197, in
> _remake_table
>     self.quote_name(model._meta.db_table),
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/backends/base/schema.py", line 110, in execute
>     cursor.execute(sql, params)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/backends/utils.py", line 79, in execute
>     return super(CursorDebugWrapper, self).execute(sql, params)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/backends/utils.py", line 64, in execute
>     return self.cursor.execute(sql, params)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/utils.py", line 95, in __exit__
>     six.reraise(dj_exc_type, dj_exc_value, traceback)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/backends/utils.py", line 64, in execute
>     return self.cursor.execute(sql, params)
>   File "/Users/alexei/.virtualenvs/djbug/lib/python2.7/site-
> packages/django/db/backends/sqlite3/base.py", line 323, in execute
>     return Database.Cursor.execute(self, query, params)
> django.db.utils.OperationalError: near ")": syntax error
> }}}
>
> Verified with Django (1.9.6 and 1.10.5) and SQLite 3 (sqlite3.version is
> 2.6.0) on macOS 10.12.2 with Python 2.7.12.

New 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
 }}}

 2. 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',),
     ),
 ]
 ...
 }}}

 3. Delete the child model (`Bar`)

 4. Generate migrations
 `migrations/0002_auto_20170123_1212.py` is:
 {{{
 ...
 operations = [
     migrations.RemoveField(
         model_name='bar',
         name='foo_ptr',
     ),
     migrations.DeleteModel(
         name='Bar',
     ),
 ]
 ...
 }}}

 5. Run migrations
 {{{
 $ python manage.py migrate
 Operations to perform:
   Apply all migrations: admin, contenttypes, foo, auth, sessions
 Running migrations:
   Rendering model states... DONE
   Applying foo.0002_auto_20170123_1212...Traceback (most recent call
 last):
   File "manage.py", line 10, in <module>
     execute_from_command_line(sys.argv)
   File "/djbug/lib/python2.7/site-
 packages/django/core/management/__init__.py", line 353, in
 execute_from_command_line
     utility.execute()
   File "/djbug/lib/python2.7/site-
 packages/django/core/management/__init__.py", line 345, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/djbug/lib/python2.7/site-
 packages/django/core/management/base.py", line 348, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/djbug/lib/python2.7/site-
 packages/django/core/management/base.py", line 399, in execute
     output = self.handle(*args, **options)
   File "/djbug/lib/python2.7/site-
 packages/django/core/management/commands/migrate.py", line 200, in handle
     executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
   File "/djbug/lib/python2.7/site-
 packages/django/db/migrations/executor.py", line 92, in migrate
     self._migrate_all_forwards(plan, full_plan, fake=fake,
 fake_initial=fake_initial)
   File "/djbug/lib/python2.7/site-
 packages/django/db/migrations/executor.py", line 121, in
 _migrate_all_forwards
     state = self.apply_migration(state, migration, fake=fake,
 fake_initial=fake_initial)
   File "/djbug/lib/python2.7/site-
 packages/django/db/migrations/executor.py", line 198, in apply_migration
     state = migration.apply(state, schema_editor)
   File "/djbug/lib/python2.7/site-
 packages/django/db/migrations/migration.py", line 123, in apply
     operation.database_forwards(self.app_label, schema_editor, old_state,
 project_state)
   File "/djbug/lib/python2.7/site-
 packages/django/db/migrations/operations/fields.py", line 121, in
 database_forwards
     schema_editor.remove_field(from_model,
 from_model._meta.get_field(self.name))
   File "/djbug/lib/python2.7/site-
 packages/django/db/backends/sqlite3/schema.py", line 247, in remove_field
     self._remake_table(model, delete_fields=[field])
   File "/djbug/lib/python2.7/site-
 packages/django/db/backends/sqlite3/schema.py", line 197, in _remake_table
     self.quote_name(model._meta.db_table),
   File "/djbug/lib/python2.7/site-
 packages/django/db/backends/base/schema.py", line 110, in execute
     cursor.execute(sql, params)
   File "/djbug/lib/python2.7/site-packages/django/db/backends/utils.py",
 line 79, in execute
     return super(CursorDebugWrapper, self).execute(sql, params)
   File "/djbug/lib/python2.7/site-packages/django/db/backends/utils.py",
 line 64, in execute
     return self.cursor.execute(sql, params)
   File "/djbug/lib/python2.7/site-packages/django/db/utils.py", line 95,
 in __exit__
     six.reraise(dj_exc_type, dj_exc_value, traceback)
   File "/djbug/lib/python2.7/site-packages/django/db/backends/utils.py",
 line 64, in execute
     return self.cursor.execute(sql, params)
   File "/djbug/lib/python2.7/site-
 packages/django/db/backends/sqlite3/base.py", line 323, in execute
     return Database.Cursor.execute(self, query, params)
 django.db.utils.OperationalError: near ")": syntax error
 }}}

 Verified with Django (1.9.6 and 1.10.5) and SQLite 3 (sqlite3.version is
 2.6.0) on macOS 10.12.2 with Python 2.7.12.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27765#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.a7f367aa3e7af7352f7c2c7d77fa2e0f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to