From e4684220af058789a9d2673a3503d2f88d8aa94a Mon Sep 17 00:00:00 2001 From: Ngalim Siregar Date: Wed, 31 Jul 2019 19:02:15 +0700 Subject: [PATCH] Fixed #30664 -- Fixed migrations crash when altering table on SQLite or altering AutoField/BigAutoField on PostgreSQL for models with quoted db_table. --- django/db/backends/postgresql/schema.py | 3 ++- django/db/backends/sqlite3/schema.py | 3 ++- tests/schema/tests.py | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py index 82e91ce8401..9e07bccf8b8 100644 --- a/django/db/backends/postgresql/schema.py +++ b/django/db/backends/postgresql/schema.py @@ -2,6 +2,7 @@ import psycopg2 from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.backends.ddl_references import IndexColumns +from django.db.backends.utils import strip_quotes class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): @@ -67,7 +68,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): if self._field_data_type(old_field) != self._field_data_type(new_field): self.sql_alter_column_type += ' USING %(column)s::%(type)s' # Make ALTER TYPE with SERIAL make sense. - table = model._meta.db_table + table = strip_quotes(model._meta.db_table) if new_type.lower() in ("serial", "bigserial"): column = new_field.column sequence_name = "%s_%s_seq" % (table, column) diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index b1c91dba3c1..029e22e1888 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.apps.registry import Apps from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.backends.ddl_references import Statement +from django.db.backends.utils import strip_quotes from django.db.models import UniqueConstraint from django.db.transaction import atomic from django.db.utils import NotSupportedError @@ -263,7 +264,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): body_copy = copy.deepcopy(body) meta_contents = { 'app_label': model._meta.app_label, - 'db_table': 'new__%s' % model._meta.db_table, + 'db_table': 'new__%s' % strip_quotes(model._meta.db_table), 'unique_together': unique_together, 'index_together': index_together, 'indexes': indexes, diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 2a22e9dec32..287ef200da7 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -649,6 +649,24 @@ class SchemaTests(TransactionTestCase): editor.remove_field(Author, Author._meta.get_field('id')) editor.alter_field(Author, old_field, new_field, strict=True) + @isolate_apps('schema') + def test_alter_primary_key_quoted_db_table(self): + class Foo(Model): + class Meta: + app_label = 'schema' + db_table = '"foo"' + + with connection.schema_editor() as editor: + editor.create_model(Foo) + self.isolated_local_models = [Foo] + old_field = Foo._meta.get_field('id') + new_field = BigAutoField(primary_key=True) + new_field.model = Foo + new_field.set_attributes_from_name('id') + with connection.schema_editor() as editor: + editor.alter_field(Foo, old_field, new_field, strict=True) + Foo.objects.create() + def test_alter_text_field(self): # Regression for "BLOB/TEXT column 'info' can't have a default value") # on MySQL.