#33462: Altering type of referenced primary key with MTI causes duplicated
operations.
---------------------------------+------------------------------------
Reporter: bcail | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Mariusz Felisiak):
* cc: David Wobrock (added)
* version: dev => 4.0
* severity: Normal => Release blocker
Comment:
Thanks for the report! This is caused by multiple references to the same
table (MTI and foreign keys). I was able to fix this by excluding
relations that were already yielded:
{{{#!diff
diff --git a/django/db/backends/base/schema.py
b/django/db/backends/base/schema.py
index 47fdae8fd4..c057092564 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -33,19 +33,22 @@ 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):
+def _related_non_m2m_objects(old_field, new_field, excluded=None):
# Filter out m2m objects from reverse relations.
# Return (old_relation, new_relation) tuples.
related_fields = zip(
(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)),
)
+ excluded = excluded or ()
for old_rel, new_rel in related_fields:
- yield old_rel, new_rel
- yield from _related_non_m2m_objects(
- old_rel.remote_field,
- new_rel.remote_field,
- )
+ if (old_rel, new_rel) not in excluded:
+ yield old_rel, new_rel
+ yield from _related_non_m2m_objects(
+ old_rel.remote_field,
+ new_rel.remote_field,
+ excluded=(*related_fields, *excluded),
+ )
class BaseDatabaseSchemaEditor:
}}}
I'm not 100% satisfied with this patch.
This is really a regression in 325d7710ce9f6155bb55610ad6b4580d31263557.
--
Ticket URL: <https://code.djangoproject.com/ticket/33462#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/063.0dd7c6891b492c09bba44927cfaeddf3%40djangoproject.com.