Opened 13 years ago

Closed 11 years ago

Last modified 9 years ago

#2350 closed defect (wontfix)

ManyToManyField and db_index

Reported by: Rudolph Froger Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



When combining a db_index with a ManyToManyField, the db_index is ignored, while an index should be made on the corresponding column in the ManyToMany table. One could even agrue that it allways makes sense to put indexes on both sides of the a ManyToMany table, but that would be to much magic: explicit is better than implicit. Anyhow:

class Foo(models.Model):
    users = ManyToManyField(User, db_index=True)

Doesn't give indexes and doesn't warn you that no index will be created.

Change History (6)

comment:1 Changed 12 years ago by (none)

milestone: Version 1.0

Milestone Version 1.0 deleted

comment:2 Changed 12 years ago by Chris Beaven

Triage Stage: UnreviewedDesign decision needed

comment:3 Changed 11 years ago by Jacob

Resolution: wontfix
Status: newclosed

ManyToManyField isn't really a field -- it's an intermediary table -- so creating a db_index on it doesn't make sense.

comment:4 Changed 11 years ago by craig.destigter@…

Resolution: wontfix
Status: closedreopened

The problem here is that there is no way to create a ManyToManyField table with indexed columns. Because both fields of such a table are used in joins, this makes any usage of the table very slow.

I agree that db_index on an intermediary table doesn't make much sense, but it's better than nothing.

Possible solutions:

  • Make db_index=True create indexes on both foreign keys in the intermediary table.
  • Use a different argument to accomplish the same thing more explicitly, i.e. index_columns=True .
  • Implicitly create indexes on both the foreign keys.

I prefer the second option since it's the most explicit.

comment:5 Changed 11 years ago by Malcolm Tredinnick

Resolution: wontfix
Status: reopenedclosed

As per contributing.txt, please do not reopen tickets that have been closed by a core developer. Start a discussion with reasons (beyond "I disagree" and allowing for the fact that this isn't impossible at all, since you can create the indexes with initial SQL if you need them) on django-dev.

Reclosing so the procedure is followed and we don't have unnecessarily open tickets.

comment:6 Changed 9 years ago by hgeerts@…

As of [11710] ManyToManyFields use a generated through model which by default creates an index for foreign keys.

Note: See TracTickets for help on using tickets.
Back to Top