db_index=True not creating indexes

57 views
Skip to first unread message

Web Architect

unread,
Oct 5, 2018, 8:14:06 AM10/5/18
to Django users
Hi,

We are using django for our ecommerce website. 

I am facing a strange issue. I was trying to index a field in a table and hence, set db_index=True in the field declaration. I ran migrations but when I checked the indexes for the table, the index for the field wasn't there. There were also few foreign key fields but no indexes for them as well. The migration didn't throw any error. Hence, I am a bit perplexed. 

Would appreciate if anyone could help me with the above. 

Also, can I create indexes directly in the database (not via django)? Will that have any issue in Django. 

I am using Django 1.11. MySQL is 5.6. 

Thanks. 

Matthew Pava

unread,
Oct 5, 2018, 9:42:18 AM10/5/18
to django...@googlegroups.com

Hi,

I’m using Django 2.0 and PostgreSQL 9.something, but my tables do have indexes.  Did you run the makemigrations command before running the migrate command?

 

Also, you can modify the database that Django uses however you want with some caveats.  Anyway, you can add views and modify columns and add constraints in the database.  I do have a suggestion that if you do that, you create your own migration files with the RunSQL command so that you can be able to recreate the database structure and keep track of your modifications.

 

So, in your case, if you really want to add an index manually, create a blank migration file, and add this operation:

migrations.RunSQL(“CREATE INDEX my_cool_looking_index USING BTREE ON appname_modelname (my_cool_looking_field)”)

 

https://docs.djangoproject.com/en/1.11/ref/django-admin/#django-admin-makemigrations

 

https://docs.djangoproject.com/en/1.11/ref/migration-operations/#runsql

 

Good luck!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2b5b6f3d-a704-46f6-9506-c0a47cdead26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Web Architect

unread,
Oct 5, 2018, 10:11:15 AM10/5/18
to Django users
Hi Mathew,

Thanks for your response. 

I did run makemigrations before running migration. There were no issues with those. But somehow the indexes were not showing on the tables when I ran SHOW INDEX from tablename;. That's the reason I was thinking of creating the indexes manually. 

I will look into RunSQL.

Thanks.


On Friday, October 5, 2018 at 7:12:18 PM UTC+5:30, Matthew Pava wrote:

Hi,

I’m using Django 2.0 and PostgreSQL 9.something, but my tables do have indexes.  Did you run the makemigrations command before running the migrate command?

 

Also, you can modify the database that Django uses however you want with some caveats.  Anyway, you can add views and modify columns and add constraints in the database.  I do have a suggestion that if you do that, you create your own migration files with the RunSQL command so that you can be able to recreate the database structure and keep track of your modifications.

 

So, in your case, if you really want to add an index manually, create a blank migration file, and add this operation:

migrations.RunSQL(“CREATE INDEX my_cool_looking_index USING BTREE ON appname_modelname (my_cool_looking_field)”)

 

https://docs.djangoproject.com/en/1.11/ref/django-admin/#django-admin-makemigrations

 

https://docs.djangoproject.com/en/1.11/ref/migration-operations/#runsql

 

Good luck!

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Web Architect
Sent: Friday, October 5, 2018 7:14 AM
To: Django users
Subject: db_index=True not creating indexes

 

Hi,

 

We are using django for our ecommerce website. 

 

I am facing a strange issue. I was trying to index a field in a table and hence, set db_index=True in the field declaration. I ran migrations but when I checked the indexes for the table, the index for the field wasn't there. There were also few foreign key fields but no indexes for them as well. The migration didn't throw any error. Hence, I am a bit perplexed. 

 

Would appreciate if anyone could help me with the above. 

 

Also, can I create indexes directly in the database (not via django)? Will that have any issue in Django. 

 

I am using Django 1.11. MySQL is 5.6. 

 

Thanks. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to djang...@googlegroups.com.

Jason

unread,
Oct 5, 2018, 11:10:56 AM10/5/18
to Django users
depending on the index and the fied, you might have to create the custom index yourself with runsql in a migration

Reply all
Reply to author
Forward
0 new messages