Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Django name the index automatically created for foreign keys columns?

Tags:

mysql

django

I know that Django automatically generates indexes for foreign keys unless we define the field with db_index=False. I have read it in django doc

But I don't know if it is possible to choose the index name, or how django chooses it.

It's always something like "tablename_xxxxxx".

"xxxxx" are like random characters?

EDITED: I've discovered that "xxxx" is some codification from the model field name, but I still don't know whether we can choose an explicit name

like image 305
toscanelli Avatar asked May 06 '16 14:05

toscanelli


2 Answers

As far as I know, there's no way to choose the name of the index. It's actually dynamically computed and it implies the hash of the table name and the column names.

See for instance the code source here, even though it's for a particular version of Django, I'm not aware of any changes towards a user named index.

like image 94
Seb D. Avatar answered Oct 31 '22 19:10

Seb D.


Is it possible to choose the index name? yes but there isn't any benefit in doing so.

The first step is to switch off the constraint, then you need to manually add one into the migration in the form of a migrations.RunSQL yes, it's a lot of hard work and totally not worth doing.

like image 26
e4c5 Avatar answered Oct 31 '22 21:10

e4c5