From 4b2cf1cd27587a30b3b081091627d7ee13141afe Mon Sep 17 00:00:00 2001 From: Alex Hill Date: Tue, 29 Mar 2016 16:58:04 +0800 Subject: [PATCH] Fixed #26384 -- Fixed renaming the PK on a model with a self-referential FK on SQLite. --- django/db/backends/sqlite3/schema.py | 10 +++++++++- docs/releases/1.9.5.txt | 3 +++ tests/schema/models.py | 5 +++++ tests/schema/tests.py | 15 +++++++++++++-- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index 6f0c8184ad2..9d9eb3c3ba2 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -76,8 +76,16 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): 3. copy the data from the old renamed table to the new table 4. delete the "app_model__old" table """ + # Self-referential fields must be recreated rather than copied from + # the old model to ensure their remote_field.field_name doesn't refer + # to an altered field. + def is_self_referential(f): + return f.is_relation and f.remote_field.model is model # Work out the new fields dict / mapping - body = {f.name: f for f in model._meta.local_concrete_fields} + body = { + f.name: f.clone() if is_self_referential(f) else f + for f in model._meta.local_concrete_fields + } # Since mapping might mix column names and default values, # its values must be already quoted. mapping = {f.column: self.quote_name(f.column) for f in model._meta.local_concrete_fields} diff --git a/docs/releases/1.9.5.txt b/docs/releases/1.9.5.txt index 7c50e4883d2..fc2c6cea032 100644 --- a/docs/releases/1.9.5.txt +++ b/docs/releases/1.9.5.txt @@ -43,3 +43,6 @@ Bugfixes * Fixed a regression with abstract model inheritance and explicit parent links (:ticket:`26413`). + +* Fixed a migrations crash on SQLite when renaming the primary key of a model + containing a ``ForeignKey`` to ``'self'`` (:ticket:`26384`). diff --git a/tests/schema/models.py b/tests/schema/models.py index adfa5054203..7a0c452d62a 100644 --- a/tests/schema/models.py +++ b/tests/schema/models.py @@ -177,3 +177,8 @@ class UniqueTest(models.Model): class Meta: apps = new_apps unique_together = ["year", "slug"] + + +class Node(models.Model): + node_id = models.AutoField(primary_key=True) + parent = models.ForeignKey('self', models.CASCADE, null=True, blank=True) diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 4cacaab6358..7882014c094 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -27,8 +27,8 @@ from .fields import ( from .models import ( Author, AuthorWithDefaultHeight, AuthorWithEvenLongerName, Book, BookForeignObj, BookWeak, BookWithLongName, BookWithO2O, BookWithoutAuthor, - BookWithSlug, IntegerPK, Note, NoteRename, Tag, TagIndexed, TagM2MTest, - TagUniqueRename, Thing, UniqueTest, new_apps, + BookWithSlug, IntegerPK, Node, Note, NoteRename, Tag, TagIndexed, + TagM2MTest, TagUniqueRename, Thing, UniqueTest, new_apps, ) @@ -1819,3 +1819,14 @@ class SchemaTests(TransactionTestCase): self.get_constraints_for_column(Tag, 'slug'), ['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key'] ) + + def test_alter_pk_with_self_referential_field(self): + """ + Changing the primary key field name of a model with a self-referential + foreign key (#26384). + """ + old_field = Node._meta.get_field('node_id') + new_field = AutoField(primary_key=True) + new_field.set_attributes_from_name('id') + with connection.schema_editor() as editor: + editor.alter_field(Node, old_field, new_field)