Opened 9 years ago

Closed 7 years ago

Last modified 5 years ago

#2350 closed defect (wontfix)

ManyToManyField and db_index

Reported by: Rudolph 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: UI/UX:

Description

Hi,

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 8 years ago by anonymous

  • milestone Version 1.0 deleted

Milestone Version 1.0 deleted

comment:2 Changed 8 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 7 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

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 7 years ago by craig.destigter@…

  • Resolution wontfix deleted
  • Status changed from closed to reopened

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 7 years ago by mtredinnick

  • Resolution set to wontfix
  • Status changed from reopened to closed

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 5 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