From 5ab86809832726957dd6f0eb8e17a461f0a9be84 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 1 Jun 2015 17:06:54 -0400 Subject: [PATCH] Fixed #24892 -- Fixed quoting of SQL when renaming a field to AutoField in PostgreSQL --- .../db/backends/postgresql_psycopg2/schema.py | 16 +++++------ docs/releases/1.8.3.txt | 3 ++ tests/schema/models.py | 8 ++++++ tests/schema/tests.py | 28 +++++++++++++++---- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/django/db/backends/postgresql_psycopg2/schema.py b/django/db/backends/postgresql_psycopg2/schema.py index 8889b3fbfb..85d9517c13 100644 --- a/django/db/backends/postgresql_psycopg2/schema.py +++ b/django/db/backends/postgresql_psycopg2/schema.py @@ -52,31 +52,31 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): [ ( self.sql_delete_sequence % { - "sequence": sequence_name, + "sequence": self.quote_name(sequence_name), }, [], ), ( self.sql_create_sequence % { - "sequence": sequence_name, + "sequence": self.quote_name(sequence_name), }, [], ), ( self.sql_alter_column % { - "table": table, + "table": self.quote_name(table), "changes": self.sql_alter_column_default % { - "column": column, - "default": "nextval('%s')" % sequence_name, + "column": self.quote_name(column), + "default": "nextval('%s')" % self.quote_name(sequence_name), } }, [], ), ( self.sql_set_sequence_max % { - "table": table, - "column": column, - "sequence": sequence_name, + "table": self.quote_name(table), + "column": self.quote_name(column), + "sequence": self.quote_name(sequence_name), }, [], ), diff --git a/docs/releases/1.8.3.txt b/docs/releases/1.8.3.txt index 860191db1b..a45ba7d268 100644 --- a/docs/releases/1.8.3.txt +++ b/docs/releases/1.8.3.txt @@ -31,3 +31,6 @@ Bugfixes * Fixed a crash when using a reverse one-to-one relation in ``ModelAdmin.list_display`` (:ticket:`24851`). + +* Fixed quoting of SQL when renaming a field to ``AutoField`` in PostgreSQL + (:ticket:`24892`). diff --git a/tests/schema/models.py b/tests/schema/models.py index d50caf9a98..ab5dd6087b 100644 --- a/tests/schema/models.py +++ b/tests/schema/models.py @@ -80,6 +80,14 @@ class BookWithSlug(models.Model): db_table = "schema_book" +class IntegerPK(models.Model): + i = models.IntegerField(primary_key=True) + + class Meta: + apps = new_apps + db_table = "INTEGERPK" # uppercase to ensure proper quoting + + class Note(models.Model): info = models.TextField() diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 2c4e79f98d..42e7e679ab 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -8,8 +8,8 @@ from django.db import ( ) from django.db.models import Model from django.db.models.fields import ( - BigIntegerField, BinaryField, BooleanField, CharField, DateTimeField, - IntegerField, PositiveIntegerField, SlugField, TextField, + AutoField, BigIntegerField, BinaryField, BooleanField, CharField, + DateTimeField, IntegerField, PositiveIntegerField, SlugField, TextField, ) from django.db.models.fields.related import ( ForeignKey, ManyToManyField, OneToOneField, @@ -22,8 +22,8 @@ from .fields import ( ) from .models import ( Author, AuthorWithDefaultHeight, AuthorWithEvenLongerName, Book, BookWeak, - BookWithLongName, BookWithO2O, BookWithSlug, Note, NoteRename, Tag, - TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest, new_apps, + BookWithLongName, BookWithO2O, BookWithSlug, IntegerPK, Note, NoteRename, + Tag, TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest, new_apps, ) @@ -40,8 +40,8 @@ class SchemaTests(TransactionTestCase): models = [ Author, AuthorWithDefaultHeight, AuthorWithEvenLongerName, Book, - BookWeak, BookWithLongName, BookWithO2O, BookWithSlug, Note, Tag, - TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest, + BookWeak, BookWithLongName, BookWithO2O, BookWithSlug, IntegerPK, Note, + Tag, TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest, ] # Utility functions @@ -748,6 +748,22 @@ class SchemaTests(TransactionTestCase): # field which drops the id sequence, at least on PostgreSQL. Author.objects.create(name='Foo') + def test_alter_int_pk_to_autofield_pk(self): + """ + Should be able to rename an IntegerField(primary_key=True) to + AutoField(primary_key=True). + """ + with connection.schema_editor() as editor: + editor.create_model(IntegerPK) + + old_field = IntegerPK._meta.get_field('i') + new_field = AutoField(primary_key=True) + new_field.model = IntegerPK + new_field.set_attributes_from_name('i') + + with connection.schema_editor() as editor: + editor.alter_field(IntegerPK, old_field, new_field, strict=True) + def test_rename(self): """ Tests simple altering of fields