[Django] #32674: DEFAULT_AUTO_FIELD should also be used for ManyToMany table primary keys

80 views
Skip to first unread message

Django

unread,
Apr 22, 2021, 2:24:46 PM4/22/21
to django-...@googlegroups.com
#32674: DEFAULT_AUTO_FIELD should also be used for ManyToMany table primary keys
-------------------------------------+-------------------------------------
Reporter: Cristiano | Owner: nobody
Coelho |
Type: | Status: new
Uncategorized |
Component: | Version: 3.2
Uncategorized | Keywords: DEFAULT_AUTO_FIELD
Severity: Normal | AutoField BigAutoField
Triage Stage: | ManyToManyField
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32674>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 23, 2021, 4:56:51 AM4/23/21
to django-...@googlegroups.com
#32674: DEFAULT_AUTO_FIELD changes should be detected for PKs of auto-created
intermediate M2M models.
-------------------------------------+-------------------------------------
Reporter: Cristiano Coelho | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 3.2
Severity: Release blocker | Resolution:
Keywords: DEFAULT_AUTO_FIELD | Triage Stage: Accepted
AutoField BigAutoField |
ManyToManyField |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* severity: Normal => Release blocker
* cc: Tom Forbes (added)
* component: Uncategorized => Migrations
* easy: 1 => 0
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/32674#comment:1>

Django

unread,
Apr 29, 2021, 3:40:34 AM4/29/21
to django-...@googlegroups.com
#32674: DEFAULT_AUTO_FIELD changes should be detected for PKs of auto-created
intermediate M2M models.
-------------------------------------+-------------------------------------
Reporter: Cristiano Coelho | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:

Keywords: DEFAULT_AUTO_FIELD | Triage Stage: Accepted
AutoField BigAutoField |
ManyToManyField |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Simon Charette, Andrew Godwin, Shai Berger, Markus Holtermann
(added)
* version: 3.2 => 4.0
* type: Bug => New feature
* severity: Release blocker => Normal


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32674#comment:2>

Django

unread,
Apr 29, 2021, 5:06:02 AM4/29/21
to django-...@googlegroups.com
#32674: DEFAULT_AUTO_FIELD changes should be detected for PKs of auto-created
intermediate M2M models.
-------------------------------------+-------------------------------------
Reporter: Cristiano Coelho | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: DEFAULT_AUTO_FIELD | Triage Stage: Accepted
AutoField BigAutoField |
ManyToManyField |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson):

[https://github.com/django/django/pull/14328 PR with admonition to migrate
auto-created through table PKs when changing `DEFAULT_AUTO_FIELD`].

--
Ticket URL: <https://code.djangoproject.com/ticket/32674#comment:3>

Django

unread,
Apr 29, 2021, 8:35:19 AM4/29/21
to django-...@googlegroups.com
#32674: DEFAULT_AUTO_FIELD changes should be detected for PKs of auto-created
intermediate M2M models.
-------------------------------------+-------------------------------------
Reporter: Cristiano Coelho | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: DEFAULT_AUTO_FIELD | Triage Stage: Accepted
AutoField BigAutoField |
ManyToManyField |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by cristianoccazinsp):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32674#comment:4>

Django

unread,
Apr 29, 2021, 9:13:11 AM4/29/21
to django-...@googlegroups.com
#32674: DEFAULT_AUTO_FIELD changes should be detected for PKs of auto-created
intermediate M2M models.
-------------------------------------+-------------------------------------
Reporter: Cristiano Coelho | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: DEFAULT_AUTO_FIELD | Triage Stage: Accepted
AutoField BigAutoField |
ManyToManyField |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson <carlton.gibson@…>):

In [changeset:"907d3a7ff4e12ad4ccc86af26a728007fe4d6fa2" 907d3a7]:
{{{
#!CommitTicketReference repository=""
revision="907d3a7ff4e12ad4ccc86af26a728007fe4d6fa2"
Refs #32674 -- Noted that auto-created through table PKs cannot be
automatically migrated.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32674#comment:5>

Django

unread,
Apr 29, 2021, 9:16:09 AM4/29/21
to django-...@googlegroups.com
#32674: DEFAULT_AUTO_FIELD changes should be detected for PKs of auto-created
intermediate M2M models.
-------------------------------------+-------------------------------------
Reporter: Cristiano Coelho | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: DEFAULT_AUTO_FIELD | Triage Stage: Accepted
AutoField BigAutoField |
ManyToManyField |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson <carlton.gibson@…>):

In [changeset:"bac416972df546ac58febd3990b2107d60d7f166" bac41697]:
{{{
#!CommitTicketReference repository=""
revision="bac416972df546ac58febd3990b2107d60d7f166"
[3.2.x] Refs #32674 -- Noted that auto-created through table PKs cannot be
automatically migrated.

Backport of 907d3a7ff4e12ad4ccc86af26a728007fe4d6fa2 from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32674#comment:6>

Django

unread,
Apr 29, 2021, 10:10:45 AM4/29/21
to django-...@googlegroups.com
#32674: DEFAULT_AUTO_FIELD changes should be detected for PKs of auto-created
intermediate M2M models.
-------------------------------------+-------------------------------------
Reporter: Cristiano Coelho | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: DEFAULT_AUTO_FIELD | Triage Stage: Accepted
AutoField BigAutoField |
ManyToManyField |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

> 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')`.

--
Ticket URL: <https://code.djangoproject.com/ticket/32674#comment:7>

Django

unread,
Oct 4, 2021, 5:39:14 AM10/4/21
to django-...@googlegroups.com
#32674: DEFAULT_AUTO_FIELD changes should be detected for PKs of auto-created
intermediate M2M models.
-------------------------------------+-------------------------------------
Reporter: Cristiano Coelho | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: DEFAULT_AUTO_FIELD | Triage Stage: Accepted
AutoField BigAutoField |
ManyToManyField |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by אורי):

* cc: אורי (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/32674#comment:8>

Django

unread,
Mar 18, 2024, 3:07:34 AM3/18/24
to django-...@googlegroups.com
#32674: DEFAULT_AUTO_FIELD changes should be detected for PKs of auto-created
intermediate M2M models.
-------------------------------------+-------------------------------------
Reporter: Cristiano Coelho | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: DEFAULT_AUTO_FIELD | Triage Stage: Accepted
AutoField BigAutoField |
ManyToManyField |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ülgen Sarıkavak):

* cc: Ülgen Sarıkavak (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/32674#comment:9>
Reply all
Reply to author
Forward
0 new messages