From ff111ea5e39cf774b3cd4237790d2bffd2826fb0 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Thu, 1 Aug 2019 11:20:56 +0200 Subject: [PATCH] Refs #30664 -- Fixed migrations crash when altering AutoField/BigAutoField with quoted db_column on PostgreSQL. --- django/db/backends/postgresql/schema.py | 2 +- tests/schema/tests.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py index 9e07bccf8b8..b7f52ccf256 100644 --- a/django/db/backends/postgresql/schema.py +++ b/django/db/backends/postgresql/schema.py @@ -70,7 +70,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): # Make ALTER TYPE with SERIAL make sense. table = strip_quotes(model._meta.db_table) if new_type.lower() in ("serial", "bigserial"): - column = new_field.column + column = strip_quotes(new_field.column) sequence_name = "%s_%s_seq" % (table, column) col_type = "integer" if new_type.lower() == "serial" else "bigint" return ( diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 287ef200da7..a765528d500 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -636,6 +636,26 @@ class SchemaTests(TransactionTestCase): with connection.schema_editor() as editor: editor.alter_field(Author, old_field, new_field, strict=True) + @isolate_apps('schema') + def test_alter_auto_field_quoted_db_column(self): + class Foo(Model): + id = AutoField(primary_key=True, db_column='"quoted_id"') + + class Meta: + app_label = 'schema' + + 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.db_column = '"quoted_id"' + 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_not_unique_field_to_primary_key(self): # Create the table. with connection.schema_editor() as editor: