diff options
| author | Adnan Umer <u.adnan@outlook.com> | 2019-08-22 14:46:17 +0500 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-09-11 11:21:08 +0200 |
| commit | 241deed2590bcb1d8c45271d44c86eaedfb57119 (patch) | |
| tree | d3fb4a1bead27aa5ed2c936d7d1f3f656a039937 /django | |
| parent | b616908ce1ad46457c4b49fe098320bb27e09581 (diff) | |
Fixed #30591 -- Fixed recreation of foreign key constraints on MySQL when altering type of referenced unique field.
Thanks Mariusz Felisiak for tests and Matthijs Kooijman for
investigation and initial patch.
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/backends/base/schema.py | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index 3a0dc39238..98afbcc05a 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -28,12 +28,16 @@ def _is_relevant_relation(relation, altered_field): return altered_field.name in field.to_fields +def _all_related_fields(model): + return model._meta._get_fields(forward=False, reverse=True, include_hidden=True) + + def _related_non_m2m_objects(old_field, new_field): # Filter out m2m objects from reverse relations. # Return (old_relation, new_relation) tuples. return zip( - (obj for obj in old_field.model._meta.related_objects if _is_relevant_relation(obj, old_field)), - (obj for obj in new_field.model._meta.related_objects if _is_relevant_relation(obj, new_field)) + (obj for obj in _all_related_fields(old_field.model) if _is_relevant_relation(obj, old_field)), + (obj for obj in _all_related_fields(new_field.model) if _is_relevant_relation(obj, new_field)), ) @@ -753,7 +757,7 @@ class BaseDatabaseSchemaEditor: # Type alteration on primary key? Then we need to alter the column # referring to us. rels_to_update = [] - if old_field.primary_key and new_field.primary_key and old_type != new_type: + if drop_foreign_keys: rels_to_update.extend(_related_non_m2m_objects(old_field, new_field)) # Changed to become primary key? if self._field_became_primary_key(old_field, new_field): |
