db_index=True not working, and indexes in class Meta yes

330 views
Skip to first unread message

Gonzalo Amadio

unread,
Dec 28, 2018, 9:38:00 PM12/28/18
to Django users
I have a model like this (simplified code):

class Agreement(modes.Model):
    job
= models.ForeignKey(...)
   
class Meta:
         indexes
= (                        
             models
.Index(fields=['job']),
       
)

And when run migrations, the index is created.
BUT, after doing this:

class Agreement(modes.Model):
    job
= models.ForeignKey(..., db_index=True)

And running makemigrations, I have this on my console output:

- Remove index agreements__job_id_eb7df0_idx from agreement

Why db_index, is not working like it is supposed to work? 


Jason

unread,
Dec 29, 2018, 8:37:20 AM12/29/18
to Django users
django automatically creates an index for foreign key columns, unless you specify db_index=False for that field.

what I suspect is happening here, you're telling django that it should have two indices for that particular column covering identical areas of responsibility.

Gonzalo Amadio

unread,
Dec 30, 2018, 1:47:33 AM12/30/18
to Django users
Yes, exactly. Django doc tells they do the same. And they do, but if you do not specify db_index=False in field, both indexes are created.

Reply all
Reply to author
Forward
0 new messages