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

11 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>
Reply all
Reply to author
Forward
0 new messages