From c8c79367a26a6cdc076e5f188094dd898a043ed2 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Mon, 16 Jun 2014 00:27:14 -0700 Subject: [PATCH] Fixed #22844: Duplicate SQL for SQLite FKs --- django/db/backends/sqlite3/schema.py | 4 ++- tests/migrations/test_operations.py | 43 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index 31d72f6537..9b62b66578 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -104,7 +104,9 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): body['__module__'] = model.__module__ temp_model = type(model._meta.object_name, model.__bases__, body) - # Create a new table with that format + # Create a new table with that format. We remove things from the + # deferred SQL that match our table name, too + self.deferred_sql = [x for x in self.deferred_sql if model._meta.db_table not in x] self.create_model(temp_model) # Copy data from the old table field_maps = list(mapping.items()) diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 886d35e822..fb2bb0b785 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -156,6 +156,49 @@ class OperationTests(MigrationTestBase): self.assertEqual(len(definition[2]), 0) self.assertEqual(definition[1][0], "Pony") + def test_create_model_with_unique_after(self): + """ + Tests the CreateModel operation directly followed by an + AlterUniqueTogether (bug #22844 - sqlite remake issues) + """ + operation1 = migrations.CreateModel( + "Pony", + [ + ("id", models.AutoField(primary_key=True)), + ("pink", models.IntegerField(default=1)), + ], + ) + operation2 = migrations.CreateModel( + "Rider", + [ + ("id", models.AutoField(primary_key=True)), + ("number", models.IntegerField(default=1)), + ("pony", models.ForeignKey("test_crmoua.Pony")), + ], + ) + operation3 = migrations.AlterUniqueTogether( + "Rider", + [ + ("number", "pony"), + ], + ) + # Test the database alteration + project_state = ProjectState() + self.assertTableNotExists("test_crmoua_pony") + self.assertTableNotExists("test_crmoua_rider") + with connection.schema_editor() as editor: + new_state = project_state.clone() + operation1.state_forwards("test_crmoua", new_state) + operation1.database_forwards("test_crmoua", editor, project_state, new_state) + project_state, new_state = new_state, new_state.clone() + operation2.state_forwards("test_crmoua", new_state) + operation2.database_forwards("test_crmoua", editor, project_state, new_state) + project_state, new_state = new_state, new_state.clone() + operation3.state_forwards("test_crmoua", new_state) + operation3.database_forwards("test_crmoua", editor, project_state, new_state) + self.assertTableExists("test_crmoua_pony") + self.assertTableExists("test_crmoua_rider") + def test_create_model_m2m(self): """ Test the creation of a model with a ManyToMany field and the