﻿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
30754	Partial indexes break future migrations in sqlite	Pēteris Caune	Simon Charette	"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""."	Bug	closed	Migrations	2.2	Release blocker	fixed	sqlite		Accepted	1	0	0	0	0	0
