#30754: Partial indexes break future migrations in sqlite
--------------------------------------+------------------------
               Reporter:  cuu508      |          Owner:  nobody
                   Type:  Bug         |         Status:  new
              Component:  Migrations  |        Version:  2.2
               Severity:  Normal      |       Keywords:
           Triage Stage:  Unreviewed  |      Has patch:  0
    Needs documentation:  0           |    Needs tests:  0
Patch needs improvement:  0           |  Easy pickings:  0
                  UI/UX:  0           |
--------------------------------------+------------------------
 How to reproduce:

 1. Create a dummy "Question" model (lifted from Django's tutorial)
 2. Add a partial index on one of its fields, create a migration and apply
 it
 3. Add another field to the model, create the migration (works) and apply
 it (throws an error)

 The error I get looks like this:

 {{{
 $ ./manage.py migrate
 Operations to perform:
   Apply all migrations: admin, auth, contenttypes, polls, sessions
 Running migrations:
   Applying polls.0003_question_hint...Traceback (most recent call last):
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/sqlite3/base.py", line 383, in execute
     return Database.Cursor.execute(self, query, params)
 sqlite3.OperationalError: no such column:
 new__polls_question.question_text

 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):
   File "./manage.py", line 21, in <module>
     main()
   File "./manage.py", line 17, in main
     execute_from_command_line(sys.argv)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/core/management/__init__.py", line 381, in
 execute_from_command_line
     utility.execute()
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/core/management/__init__.py", line 375, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/core/management/base.py", line 323, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/core/management/base.py", line 364, in execute
     output = self.handle(*args, **options)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/core/management/base.py", line 83, in wrapped
     res = handle_func(*args, **kwargs)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/core/management/commands/migrate.py", line 234, in handle
     fake_initial=fake_initial,
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/migrations/executor.py", line 117, in migrate
     state = self._migrate_all_forwards(state, plan, full_plan, fake=fake,
 fake_initial=fake_initial)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/migrations/executor.py", line 147, in
 _migrate_all_forwards
     state = self.apply_migration(state, migration, fake=fake,
 fake_initial=fake_initial)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/migrations/executor.py", line 245, in apply_migration
     state = migration.apply(state, schema_editor)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/migrations/migration.py", line 124, in apply
     operation.database_forwards(self.app_label, schema_editor, old_state,
 project_state)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/migrations/operations/fields.py", line 112, in
 database_forwards
     field,
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/sqlite3/schema.py", line 327, in add_field
     self._remake_table(model, create_field=field)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/sqlite3/schema.py", line 300, in _remake_table
     self.execute(sql)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/base/schema.py", line 137, in execute
     cursor.execute(sql, params)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/utils.py", line 99, in execute
     return super().execute(sql, params)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/utils.py", line 67, in execute
     return self._execute_with_wrappers(sql, params, many=False,
 executor=self._execute)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
     return executor(sql, params, many, context)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
   File "/tmp/htemp/lib/python3.7/site-packages/django/db/utils.py", line
 89, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/sqlite3/base.py", line 383, in execute
     return Database.Cursor.execute(self, query, params)
 django.db.utils.OperationalError: no such column:
 new__polls_question.question_text
 }}}

 Note that the migration creating an partial index works. It's the *next*
 migration that fails.

 For me, this happens only with SQLite, no errors with PostgreSQL. Also no
 problems with MySQL, which does not support partial indexes.
 If I remove the `condition` clause (i.e., create a regular index instead
 of an partial index) then it works fine.

 Here's an isolated test-case, I've added the three steps in 3 separate
 commits: https://github.com/cuu508/sqlite_partial_indexes

 I patched `django/db/backends/sqlite3/base.py` to print SQL queries to
 stdout. The relevant part:

 {{{
 DROP TABLE "polls_question"
 ALTER TABLE "new__polls_question" RENAME TO "polls_question"
 CREATE INDEX "polls_nonempty_pub_date" ON "polls_question" ("pub_date")
 WHERE NOT ("new__polls_question"."question_text" = '')
 Traceback (most recent call last):
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
   File "/tmp/htemp/lib/python3.7/site-
 packages/django/db/backends/sqlite3/base.py", line 420, in execute
     return Database.Cursor.execute(self, query, params)
 sqlite3.OperationalError: no such column:
 new__polls_question.question_text
 }}}

 It appears to be renaming the table, and then trying to use it by its old
 name.
 Apologies if this is already reported – couldn't find a similar ticket
 with a quick search for "sqlite".

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30754>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.12e1a4d4845c76c0c299a086cb1f7859%40djangoproject.com.

Reply via email to