Refs #32743 -- Fixed recreation of foreign key constraints when altering type of referenced primary key with MTI.

Follow up to 325d7710ce.
This commit is contained in:
Jordan Bae 2021-07-26 23:56:05 +09:00 committed by Mariusz Felisiak
parent 11879530a3
commit 3d9040a50b
3 changed files with 25 additions and 2 deletions

View File

@ -483,6 +483,7 @@ answer newbie questions, and generally made Django that much better:
Jonathan Daugherty (cygnus) <http://www.cprogrammer.org/> Jonathan Daugherty (cygnus) <http://www.cprogrammer.org/>
Jonathan Feignberg <jdf@pobox.com> Jonathan Feignberg <jdf@pobox.com>
Jonathan Slenders Jonathan Slenders
Jordan Bae <qoentlr37@gmail.com>
Jordan Dimov <s3x3y1@gmail.com> Jordan Dimov <s3x3y1@gmail.com>
Jordi J. Tablada <jordi.joan@gmail.com> Jordi J. Tablada <jordi.joan@gmail.com>
Jorge Bastida <me@jorgebastida.com> Jorge Bastida <me@jorgebastida.com>

View File

@ -849,8 +849,8 @@ class BaseDatabaseSchemaEditor:
self.execute(self._create_fk_sql(model, new_field, "_fk_%(to_table)s_%(to_column)s")) self.execute(self._create_fk_sql(model, new_field, "_fk_%(to_table)s_%(to_column)s"))
# Rebuild FKs that pointed to us if we previously had to drop them # Rebuild FKs that pointed to us if we previously had to drop them
if drop_foreign_keys: if drop_foreign_keys:
for rel in new_field.model._meta.related_objects: for _, rel in rels_to_update:
if _is_relevant_relation(rel, new_field) and rel.field.db_constraint: if rel.field.db_constraint:
self.execute(self._create_fk_sql(rel.related_model, rel.field, "_fk")) self.execute(self._create_fk_sql(rel.related_model, rel.field, "_fk"))
# Does it have check constraints we need to add? # Does it have check constraints we need to add?
if old_db_params['check'] != new_db_params['check'] and new_db_params['check']: if old_db_params['check'] != new_db_params['check'] and new_db_params['check']:

View File

@ -1551,10 +1551,32 @@ class OperationTests(OperationTestBase):
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
operation.database_forwards(app_label, editor, project_state, new_state) operation.database_forwards(app_label, editor, project_state, new_state)
assertIdTypeEqualsMTIFkType() assertIdTypeEqualsMTIFkType()
if connection.features.supports_foreign_keys:
self.assertFKExists(
f'{app_label}_shetlandpony',
['pony_ptr_id'],
(f'{app_label}_pony', 'id'),
)
self.assertFKExists(
f'{app_label}_shetlandrider',
['pony_id'],
(f'{app_label}_shetlandpony', 'pony_ptr_id'),
)
# Reversal. # Reversal.
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
operation.database_backwards(app_label, editor, new_state, project_state) operation.database_backwards(app_label, editor, new_state, project_state)
assertIdTypeEqualsMTIFkType() assertIdTypeEqualsMTIFkType()
if connection.features.supports_foreign_keys:
self.assertFKExists(
f'{app_label}_shetlandpony',
['pony_ptr_id'],
(f'{app_label}_pony', 'id'),
)
self.assertFKExists(
f'{app_label}_shetlandrider',
['pony_id'],
(f'{app_label}_shetlandpony', 'pony_ptr_id'),
)
@skipUnlessDBFeature('supports_foreign_keys') @skipUnlessDBFeature('supports_foreign_keys')
def test_alter_field_reloads_state_on_fk_with_to_field_target_type_change(self): def test_alter_field_reloads_state_on_fk_with_to_field_target_type_change(self):