Opened 18 years ago

Closed 16 years ago

Last modified 15 years ago

#2350 closed defect (wontfix)

ManyToManyField and db_index

Reported by: Rudolph Froger Owned by: nobody
Component: Database layer (models, ORM) Version: dev
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

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 by (none), 18 years ago

milestone: Version 1.0

Milestone Version 1.0 deleted

comment:2 by Chris Beaven, 18 years ago

Triage Stage: UnreviewedDesign decision needed

comment:3 by Jacob, 17 years ago

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

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 by Malcolm Tredinnick, 16 years ago

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 by hgeerts@…, 15 years ago

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