#36061: Custom data migration for M2M with through_fields not working
-------------------------------------+-------------------------------------
Reporter: Brian Nettleton | Owner: (none)
Type: Bug | Status: new
Component: Migrations | Version: 4.2
Severity: Normal | Resolution:
Keywords: through_fields | Triage Stage: Accepted
migration |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):
* stage: Unreviewed => Accepted
Comment:
I find it hard to believe but `through_fields` never actually worked in
migrations since its introduction in
c627da0ccc12861163f28177aa7538b420a9d310 (#14549) as
`ManyToManyField.deconstruct`
[https://github.com/django/django/blob/c627da0ccc12861163f28177aa7538b420a9d310/django/db/models/fields/related.py#L2023-L2049
never special cased it]. While the lack of support for `through` (which is
by definition more common) was
[https://github.com/django/django/commit/6b078044745ee3c665051a886021c6fd1f6873b6
noticed during the introduction of migrations] `through_fields` was likely
missed as both features landed around the same time in the 1.7 release
cycle.
Brian, given you've already spent time investigating where
`through_fields` is stored (from your provided reproduction case) would
you be interested in submitting a patch? Adjusting
`ManyToManyField.deconstruct` to include `through_fields` if provided like
so
{{{#!diff
diff --git a/django/db/models/fields/related.py
b/django/db/models/fields/related.py
index 9ef2d29024..91018a27ff 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -1772,6 +1772,8 @@ def deconstruct(self):
kwargs["through"] = self.remote_field.through
elif not self.remote_field.through._meta.auto_created:
kwargs["through"] = self.remote_field.through._meta.label
+ if through_fields := getattr(self.remote_field, "through_fields",
None):
+ kwargs["through_fields"] = through_fields
# If swappable is True, then see if we're actually pointing to
the target
# of a swap.
swappable_setting = self.swappable_setting
}}}
With extra assertions in
[https://github.com/django/django/blob/51df0dff3c4f28016185a9e876ee5b3420712f99/tests/field_deconstruction/tests.py#L498-L550
the associated many-to-many field deconstruction test] should do.
--
Ticket URL: <https://code.djangoproject.com/ticket/36061#comment:1>
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 visit
https://groups.google.com/d/msgid/django-updates/0107019433772f19-9ee3f5ce-7428-4726-9bc8-52936474694b-000000%40eu-central-1.amazonses.com.