diff options
| author | Dan Tao <daniel.tao@gmail.com> | 2019-01-18 22:17:26 -0600 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2019-01-29 15:42:57 -0500 |
| commit | 738faf9da2a5cd03148a36375db80746c99c9623 (patch) | |
| tree | f6b31d0e5c571693f772fe07ef224ebf5a217a98 /tests | |
| parent | 9a0cc54524422dbdd9213e83a8ad7e8a4c13bd3e (diff) | |
Fixed #30108 -- Allowed adding foreign key constraints in the same statement that adds a field.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/indexes/tests.py | 8 | ||||
| -rw-r--r-- | tests/schema/tests.py | 21 |
2 files changed, 24 insertions, 5 deletions
diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py index 7eb5dd89a9..ecc449ed4a 100644 --- a/tests/indexes/tests.py +++ b/tests/indexes/tests.py @@ -226,11 +226,9 @@ class SchemaIndexesMySQLTests(TransactionTestCase): new_field.set_attributes_from_name('new_foreign_key') editor.add_field(ArticleTranslation, new_field) field_created = True - self.assertEqual([str(statement) for statement in editor.deferred_sql], [ - 'ALTER TABLE `indexes_articletranslation` ' - 'ADD CONSTRAINT `indexes_articletrans_new_foreign_key_id_d27a9146_fk_indexes_a` ' - 'FOREIGN KEY (`new_foreign_key_id`) REFERENCES `indexes_article` (`id`)' - ]) + # No deferred SQL. The FK constraint is included in the + # statement to add the field. + self.assertFalse(editor.deferred_sql) finally: if field_created: with connection.schema_editor() as editor: diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 410a52b646..9b40a43523 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -241,6 +241,27 @@ class SchemaTests(TransactionTestCase): editor.alter_field(Book, old_field, new_field, strict=True) self.assertForeignKeyExists(Book, 'author_id', 'schema_tag') + @skipUnlessDBFeature('can_create_inline_fk') + def test_inline_fk(self): + # Create some tables. + with connection.schema_editor() as editor: + editor.create_model(Author) + editor.create_model(Book) + editor.create_model(Note) + self.assertForeignKeyNotExists(Note, 'book_id', 'schema_book') + # Add a foreign key from one to the other. + with connection.schema_editor() as editor: + new_field = ForeignKey(Book, CASCADE) + new_field.set_attributes_from_name('book') + editor.add_field(Note, new_field) + self.assertForeignKeyExists(Note, 'book_id', 'schema_book') + # Creating a FK field with a constraint uses a single statement without + # a deferred ALTER TABLE. + self.assertFalse([ + sql for sql in (str(statement) for statement in editor.deferred_sql) + if sql.startswith('ALTER TABLE') and 'ADD CONSTRAINT' in sql + ]) + @skipUnlessDBFeature('supports_foreign_keys') def test_char_field_with_db_index_to_fk(self): # Create the table |
