summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2019-12-23 23:28:59 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-12-23 23:30:30 +0100
commit0f8041abd5bf0977d28b676445d35a81c6b7b5de (patch)
treebdfb35dffd666d18cb614509aa2a5a27c334b705 /tests
parenteb40426259cbfd8c4d25797c878424542cf1a1a7 (diff)
[3.0.x] Fixed #31106 -- Fixed migrations crash on PostgreSQL 10+ when adding FK constraints inline and changing data.
This allows adding foreign key constraints inline and changing data in the same migration on PostgreSQL 10+. Regression in 738faf9da2a5cd03148a36375db80746c99c9623. Thanks Janne Rönkkö for the report and Simon Charette for the implementation idea and review. Backport of 22ce5d0031bd795ade081394043833e82046016c from master
Diffstat (limited to 'tests')
-rw-r--r--tests/schema/tests.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 72476ed643..074f252197 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -275,6 +275,43 @@ class SchemaTests(TransactionTestCase):
if sql.startswith('ALTER TABLE') and 'ADD CONSTRAINT' in sql
])
+ @skipUnlessDBFeature('can_create_inline_fk')
+ def test_add_inline_fk_update_data(self):
+ with connection.schema_editor() as editor:
+ editor.create_model(Node)
+ # Add an inline foreign key and update data in the same transaction.
+ new_field = ForeignKey(Node, CASCADE, related_name='new_fk', null=True)
+ new_field.set_attributes_from_name('new_parent_fk')
+ parent = Node.objects.create()
+ with connection.schema_editor() as editor:
+ editor.add_field(Node, new_field)
+ editor.execute('UPDATE schema_node SET new_parent_fk_id = %s;', [parent.pk])
+ self.assertIn('new_parent_fk_id', self.get_indexes(Node._meta.db_table))
+
+ @skipUnlessDBFeature(
+ 'can_create_inline_fk',
+ 'allows_multiple_constraints_on_same_fields',
+ )
+ @isolate_apps('schema')
+ def test_add_inline_fk_index_update_data(self):
+ class Node(Model):
+ class Meta:
+ app_label = 'schema'
+
+ with connection.schema_editor() as editor:
+ editor.create_model(Node)
+ # Add an inline foreign key, update data, and an index in the same
+ # transaction.
+ new_field = ForeignKey(Node, CASCADE, related_name='new_fk', null=True)
+ new_field.set_attributes_from_name('new_parent_fk')
+ parent = Node.objects.create()
+ with connection.schema_editor() as editor:
+ editor.add_field(Node, new_field)
+ Node._meta.add_field(new_field)
+ editor.execute('UPDATE schema_node SET new_parent_fk_id = %s;', [parent.pk])
+ editor.add_index(Node, Index(fields=['new_parent_fk'], name='new_parent_inline_fk_idx'))
+ self.assertIn('new_parent_fk_id', self.get_indexes(Node._meta.db_table))
+
@skipUnlessDBFeature('supports_foreign_keys')
def test_char_field_with_db_index_to_fk(self):
# Create the table