summaryrefslogtreecommitdiff
path: root/tests/schema
diff options
context:
space:
mode:
authorDan Tao <daniel.tao@gmail.com>2019-01-18 22:17:26 -0600
committerTim Graham <timograham@gmail.com>2019-01-29 15:42:57 -0500
commit738faf9da2a5cd03148a36375db80746c99c9623 (patch)
treef6b31d0e5c571693f772fe07ef224ebf5a217a98 /tests/schema
parent9a0cc54524422dbdd9213e83a8ad7e8a4c13bd3e (diff)
Fixed #30108 -- Allowed adding foreign key constraints in the same statement that adds a field.
Diffstat (limited to 'tests/schema')
-rw-r--r--tests/schema/tests.py21
1 files changed, 21 insertions, 0 deletions
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