[Django] #35877: Documentation on "Changing a ManyToManyField to use a through model" does not respect database index

17 views
Skip to first unread message

Django

unread,
Oct 30, 2024, 6:31:20 AM10/30/24
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-----------------------+-----------------------------------------
Reporter: robwa | Type: Bug
Status: new | Component: Documentation
Version: | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------+-----------------------------------------
When creating a `manytomany` field with an implicit `through` table, a
unique index is created on the database side for the `through` table for
the two foreign keys. This fact is not respected in the documentation on
[https://docs.djangoproject.com/en/dev/howto/writing-
migrations/#changing-a-manytomanyfield-to-use-a-through-model Changing a
ManyToManyField to use a through model]. When using the proposed code,
Django loses track of the index.

The addition of a simple options statement in the migration may be
sufficient:


{{{
// ...
operations = [
migrations.CreateModel(
name="AuthorBook",
// ...
options={
"unique_together": {("author", "book")},
},
),
]
}}}


I have not been able to check comprehensively how different database
systems behave when interacting with Django and in the context of renaming
the table. SQLite retains the index, but it is not renamed. In the case of
PostgreSQL, there were later bugs in an implementation that were difficult
to find.
--
Ticket URL: <https://code.djangoproject.com/ticket/35877>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 30, 2024, 8:33:22 AM10/30/24
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
--------------------------------------+------------------------------------
Reporter: robwa | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: dev
Severity: Normal | 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 Sarah Boyce):

* cc: Adam Johnson, Mariusz Felisiak (added)
* stage: Unreviewed => Accepted
* type: Bug => Cleanup/optimization
* version: => dev

Comment:

This was added as part of a9ee6872bd9e1bacc2da827dbd5b9093f724e4a5 to give
a solid example of using `SeparateDatabaseAndState`
That being said, I'm not against us adding an index and so accepting (I
think it should be a `UniqueConstraint` as we recommend folks don't use
`unique_together` anymore).
I will cc some folks who were involved in this section in the docs and see
what they think also
{{{
// ...
options={
"constraints": [
models.UniqueConstraint(
fields=["author", "book"],
name="unique_author_book",
)
]
},
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:1>

Django

unread,
Oct 31, 2024, 6:37:56 PM10/31/24
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Aditya
Type: | Chaudhary
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | 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 Aditya Chaudhary):

* owner: (none) => Aditya Chaudhary
* status: new => assigned

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

Django

unread,
Jan 17, 2025, 12:03:59 AMJan 17
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Ayush
Type: | Khatri
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | 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 Ayush Khatri ):

* owner: Aditya Chaudhary => Ayush Khatri

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

Django

unread,
Jan 17, 2025, 12:09:26 AMJan 17
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Ayush
Type: | Khatri
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ayush Khatri ):

* has_patch: 0 => 1

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

Django

unread,
Feb 8, 2025, 8:31:26 AMFeb 8
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Ayush
Type: | Khatri
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Clifford Gama):

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

Django

unread,
Mar 20, 2025, 8:09:23 AMMar 20
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Ayush
Type: | Khatri
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

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

Django

unread,
Sep 22, 2025, 1:49:03 AM (9 days ago) Sep 22
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Ayush
Type: | Khatri
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Samriddha Kumar Tripathi):

Hello, I would like to work on this ticket
--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:7>

Django

unread,
Sep 22, 2025, 1:55:49 PM (9 days ago) Sep 22
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Samriddha
Type: | Kumar Tripathi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Samriddha Kumar Tripathi):

* owner: Ayush Khatri => Samriddha Kumar Tripathi

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

Django

unread,
Sep 22, 2025, 4:01:59 PM (9 days ago) Sep 22
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Samriddha
Type: | Kumar Tripathi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Samriddha Kumar Tripathi):

PR:
https://github.com/django/django/pull/19891
--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:9>

Django

unread,
Sep 23, 2025, 12:46:06 AM (8 days ago) Sep 23
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Samriddha
Type: | Kumar Tripathi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Samriddha Kumar Tripathi):

* needs_better_patch: 1 => 0

Comment:

PR:
https://github.com/django/django/pull/19891
--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:10>

Django

unread,
Sep 23, 2025, 1:08:20 AM (8 days ago) Sep 23
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Samriddha
Type: | Kumar Tripathi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Samriddha Kumar Tripathi):

I've submitted a PR that adds a documentation note clarifying the behavior
when switching from an implicit ManyToManyField to a custom through model.
It includes a recommendation to use UniqueConstraint to preserve
uniqueness.

I've unset the "patch needs improvement" flag and ensured all doc checks
pass.

Looking forward to feedback!
--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:11>

Django

unread,
Sep 25, 2025, 8:12:13 AM (6 days ago) Sep 25
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Samriddha
Type: | Kumar Tripathi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:12>

Django

unread,
Sep 25, 2025, 3:35:41 PM (6 days ago) Sep 25
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Samriddha
Type: | Kumar Tripathi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Samriddha Kumar Tripathi):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:13>

Django

unread,
Sep 26, 2025, 3:34:10 AM (5 days ago) Sep 26
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Samriddha
Type: | Kumar Tripathi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:14>

Django

unread,
Sep 26, 2025, 8:18:33 AM (5 days ago) Sep 26
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Samriddha
Type: | Kumar Tripathi
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:15>

Django

unread,
Sep 29, 2025, 2:57:22 AM (2 days ago) Sep 29
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Samriddha
Type: | Kumar Tripathi
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"daba609a9bdc7a97bcf327c7ba0a5f7b3540b46e" daba609a]:
{{{#!CommitTicketReference repository=""
revision="daba609a9bdc7a97bcf327c7ba0a5f7b3540b46e"
Fixed #35877, Refs #36128 -- Documented unique constraint when migrating a
m2m field to use a through model.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:16>

Django

unread,
Sep 29, 2025, 2:59:49 AM (2 days ago) Sep 29
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Samriddha
Type: | Kumar Tripathi
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"956455dc8a8654bad1264c39d02bd5ffafc7733d" 956455dc]:
{{{#!CommitTicketReference repository=""
revision="956455dc8a8654bad1264c39d02bd5ffafc7733d"
[6.0.x] Fixed #35877, Refs #36128 -- Documented unique constraint when
migrating a m2m field to use a through model.

Backport of daba609a9bdc7a97bcf327c7ba0a5f7b3540b46e from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:17>

Django

unread,
Sep 29, 2025, 3:00:53 AM (2 days ago) Sep 29
to django-...@googlegroups.com
#35877: Documentation on "Changing a ManyToManyField to use a through model" does
not respect database index
-------------------------------------+-------------------------------------
Reporter: robwa | Owner: Samriddha
Type: | Kumar Tripathi
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"6f3813e4b6e3db7fa3cc612c78f898142468bca9" 6f3813e4]:
{{{#!CommitTicketReference repository=""
revision="6f3813e4b6e3db7fa3cc612c78f898142468bca9"
[5.2.x] Fixed #35877, Refs #36128 -- Documented unique constraint when
migrating a m2m field to use a through model.

Backport of daba609a9bdc7a97bcf327c7ba0a5f7b3540b46e from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35877#comment:18>
Reply all
Reply to author
Forward
0 new messages