Opened 4 years ago

Last modified 8 months ago

#32674 new New feature

DEFAULT_AUTO_FIELD changes should be detected for PKs of auto-created intermediate M2M models.

Reported by: Cristiano Coelho Owned by: nobody
Component: Migrations Version: 4.0
Severity: Normal Keywords: DEFAULT_AUTO_FIELD AutoField BigAutoField ManyToManyField
Cc: ccoelho@…, Tom Forbes, Simon Charette, Andrew Godwin, Shai Berger, Markus Holtermann, אורי, Ülgen Sarıkavak Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Django 3.2 introduced DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'. However, if we set it to BigAutoField, I would expect that the intermediary table created for ManyToMany relationships also honor this value.

Right now, we end up with tables that have FKs as bigint, but their own PK as integer. This may not be a direct bug, but will cause issues for sure.

Ideally, ManyToManyField should have an easy way to configure its PK without having to implement the through model.

Change History (9)

comment:1 by Mariusz Felisiak, 4 years ago

Cc: Tom Forbes added
Component: UncategorizedMigrations
Easy pickings: unset
Severity: NormalRelease blocker
Summary: DEFAULT_AUTO_FIELD should also be used for ManyToMany table primary keysDEFAULT_AUTO_FIELD changes should be detected for PKs of auto-created intermediate M2M models.
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Thanks for this report. I agree we should detect this change for PKs of auto-created intermediate models.

comment:2 by Mariusz Felisiak, 4 years ago

Cc: Simon Charette Andrew Godwin Shai Berger Markus Holtermann added
Severity: Release blockerNormal
Type: BugNew feature
Version: 3.24.0

I tried, I really really tried, but after few days of using different approaches I need to conclude that this is massive, extremely complicate, and maybe even unfeasible. As far as I'm aware we've never took into account changes in auto-created many-to-many tables except for those directly related with a definition of ManyToManyFields. A project state used by migrations autodetector doesn't include auto-created model, so the only fix that I can imagine would require including through_pk_type in the ManyToManyField.deconstruct() but it's also tricky and error-prone.

We're going to document this caveat and treat this ticket as a new feature.

comment:4 by Cristiano Coelho, 4 years ago

Can we at least provide more detailed migration steps? Right now, the only alternative is to go table by table and do the migration more or less manually. This is very time consuming for large projects.

comment:5 by Carlton Gibson <carlton.gibson@…>, 4 years ago

In 907d3a7:

Refs #32674 -- Noted that auto-created through table PKs cannot be automatically migrated.

comment:6 by Carlton Gibson <carlton.gibson@…>, 4 years ago

In bac41697:

[3.2.x] Refs #32674 -- Noted that auto-created through table PKs cannot be automatically migrated.

Backport of 907d3a7ff4e12ad4ccc86af26a728007fe4d6fa2 from main

comment:7 by Simon Charette, 4 years ago

A project state used by migrations autodetector doesn't include auto-created model, so the only fix that I can imagine would require including through_pk_type in the ManyToManyField.deconstruct() but it's also tricky and error-prone.

That seems like the only solution to me as well.

Having a through_pk: Field(primary_key=True) option instead of through_pk_type seems more flexible though as it would allow easier customization e.g. though_pk=UUIDField(default=uuid.uuid4, primary_key=True). I guess it could also serve as a way to define a composite primary key on the from_field and to_field if we ever manage to add support for that e.g. through_pk=CompositePrimaryKey('author', 'book').

comment:8 by אורי, 3 years ago

Cc: אורי added

comment:9 by Ülgen Sarıkavak, 8 months ago

Cc: Ülgen Sarıkavak added
Note: See TracTickets for help on using tickets.
Back to Top