#36343 closed Uncategorized (invalid)
Uprading to Django 5.2 from Django 5.1.7 creates a new migration for a self referencing through model
| Reported by: | Mark Chackerian | Owned by: | |
|---|---|---|---|
| Component: | Uncategorized | Version: | 5.2 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I have a model with a self-referential through model like
class Cad((models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
cads: models.ManyToManyField = models.ManyToManyField(
"self", symmetrical=False, through="CadChild", through_fields=("parent", "child")
)
...
class CadChild(models.Model):
"""A through model for Cad parent/child many-to-many relationships."""
parent = models.ForeignKey(
Cad,
on_delete=models.CASCADE,
related_name="parent_cad_set",
)
child = models.ForeignKey(
Cad,
on_delete=models.CASCADE,
related_name="child_cad_set",
)
When I upgraded from 5.1.7 to 5.2 I needed to created to create a new migration, which looked like:
# Generated by Django 5.2 on 2025-04-21 13:25
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('projects', '0012_documentpagetheme_documentpagethemeuserdefault'),
]
operations = [
migrations.AlterField(
model_name='cad',
name='cads',
field=models.ManyToManyField(through='projects.CadChild', through_fields=('parent', 'child'), to='projects.cad'),
),
]
This may be intended behavior but I can't find any reference to this change in the release notes.
Note:
See TracTickets
for help on using tickets.
Thank you for your report, it is effectively intended.
Prior to Django 5.2 there was a bug that prevented the appropriate tracking of
through_fieldsin migrations #36061 (b13b8684a04d0bc1081104c5973c62c27dc673b0) which could cause crashes or data corruptions if the involved models were used in data migrations (e.g. throughRunPython). You'll notice that the migration definingcadswill be lacking thethrough_fieldsyou declared in your models.We don't document bug fixes in release notes hence why you couldn't find it.