summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2019-09-08 20:31:43 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-09-10 10:04:12 +0200
commit964dd4f4f208722d8993a35c1ff047d353cea1ea (patch)
tree6f28554948b727f6213e5dd5a200586a510f9579
parentbe053c0b2a5160246e3ca76ff4802e9181ae6f7d (diff)
[2.2.x] Fixed #30754 -- Prevented inclusion of aliases in partial index conditions.
SQLite doesn't repoint table aliases in partial index conditions on table rename which breaks the documented table alteration procedure. Thanks Pēteris Caune for the report. Backport of 34decdebf157b6f05836009cc1967f74ee541fdf from master
-rw-r--r--django/db/models/indexes.py9
-rw-r--r--docs/releases/2.2.6.txt3
-rw-r--r--tests/indexes/tests.py12
3 files changed, 11 insertions, 13 deletions
diff --git a/django/db/models/indexes.py b/django/db/models/indexes.py
index a8bf089ba1..5571be3f46 100644
--- a/django/db/models/indexes.py
+++ b/django/db/models/indexes.py
@@ -59,13 +59,10 @@ class Index:
if self.condition is None:
return None
query = Query(model=model)
- query.add_q(self.condition)
+ where = query.build_where(self.condition)
compiler = query.get_compiler(connection=schema_editor.connection)
- # Only the WhereNode is of interest for the partial index.
- sql, params = query.where.as_sql(compiler=compiler, connection=schema_editor.connection)
- # BaseDatabaseSchemaEditor does the same map on the params, but since
- # it's handled outside of that class, the work is done here.
- return sql % tuple(map(schema_editor.quote_value, params))
+ sql, params = where.as_sql(compiler, schema_editor.connection)
+ return sql % tuple(schema_editor.quote_value(p) for p in params)
def create_sql(self, model, schema_editor, using=''):
fields = [model._meta.get_field(field_name) for field_name, _ in self.fields_orders]
diff --git a/docs/releases/2.2.6.txt b/docs/releases/2.2.6.txt
index e75160f8f3..59c29ef0a6 100644
--- a/docs/releases/2.2.6.txt
+++ b/docs/releases/2.2.6.txt
@@ -9,4 +9,5 @@ Django 2.2.6 fixes several bugs in 2.2.5.
Bugfixes
========
-* ...
+* Fixed migrations crash on SQLite when altering a model containing partial
+ indexes (:ticket:`30754`).
diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py
index 7eb5dd89a9..8f3c032641 100644
--- a/tests/indexes/tests.py
+++ b/tests/indexes/tests.py
@@ -108,7 +108,7 @@ class PartialIndexConditionIgnoredTests(TransactionTestCase):
editor.add_index(Article, index)
self.assertNotIn(
- 'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), 'published'),
+ 'WHERE %s' % editor.quote_name('published'),
str(index.create_sql(Article, editor))
)
@@ -260,7 +260,7 @@ class PartialIndexTests(TransactionTestCase):
)
)
self.assertIn(
- 'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")),
+ 'WHERE %s' % editor.quote_name('pub_date'),
str(index.create_sql(Article, schema_editor=editor))
)
editor.add_index(index=index, model=Article)
@@ -277,7 +277,7 @@ class PartialIndexTests(TransactionTestCase):
condition=Q(pk__gt=1),
)
self.assertIn(
- 'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name('id')),
+ 'WHERE %s' % editor.quote_name('id'),
str(index.create_sql(Article, schema_editor=editor))
)
editor.add_index(index=index, model=Article)
@@ -294,7 +294,7 @@ class PartialIndexTests(TransactionTestCase):
condition=Q(published=True),
)
self.assertIn(
- 'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name('published')),
+ 'WHERE %s' % editor.quote_name('published'),
str(index.create_sql(Article, schema_editor=editor))
)
editor.add_index(index=index, model=Article)
@@ -321,7 +321,7 @@ class PartialIndexTests(TransactionTestCase):
sql = str(index.create_sql(Article, schema_editor=editor))
where = sql.find('WHERE')
self.assertIn(
- 'WHERE (%s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")),
+ 'WHERE (%s' % editor.quote_name('pub_date'),
sql
)
# Because each backend has different syntax for the operators,
@@ -341,7 +341,7 @@ class PartialIndexTests(TransactionTestCase):
condition=Q(pub_date__isnull=False),
)
self.assertIn(
- 'WHERE %s.%s IS NOT NULL' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")),
+ 'WHERE %s IS NOT NULL' % editor.quote_name('pub_date'),
str(index.create_sql(Article, schema_editor=editor))
)
editor.add_index(index=index, model=Article)