1

I had this in my models.py

category = models.CharField(max_length=1024, null=False)

I changed it to this:

category = models.CharField(max_length=256, null=False)

and migrated successfully.

and then I changed it to this:

category = models.CharField(max_length=256, null=False, db_index=True)

While migrating I get this:

_mysql_exceptions.Warning: Specified key was too long; max key length is 767 bytes

and migration breaks.

However when I open phpmyadmin panel I see that the index has been created.

Should I care? Should I do anything not to have this warning? Is this warning important?

EDIT: field collation is utf8_general_ci

xpanta
  • 8,124
  • 15
  • 60
  • 104
  • Why would you need an index with 256 digits in length? – aIKid Nov 01 '13 at 09:10
  • Just a little strange. – aIKid Nov 01 '13 at 09:25
  • 1
    10^256 no. of indexes will be generated by 256 digits. Are you sure you could generate this amount of data. Try to be more realistic this will waste memory. And also this is not a good practice. This link will might help you further. http://stackoverflow.com/questions/4547479/error-during-djangos-syncdb-on-server – sawan gupta Nov 01 '13 at 09:29
  • which engine do you use? innodb or myisam? – Leonardo.Z Nov 01 '13 at 09:31
  • It is not important for me to have a max_length of 256. I can easily drop to 128 or even less. The question goes as to why (although I am warned) the index is created whatsoever – xpanta Nov 01 '13 at 09:35
  • 1
    @xpanta Because it's a Warning not a error. The index is still created only for the first 767 bytes of that column. – Leonardo.Z Nov 01 '13 at 09:42

1 Answers1

2

According MySQL 5.6 doc: Limits on InnoDB Tables

By default, an index key for a single-column index can be up to 767 bytes. The same length limit applies to any index key prefix.

When you attempt to specify an index prefix length longer than allowed, the length is silently reduced to the maximum length for a nonunique index.

For a unique index, exceeding the index prefix limit produces an error. To avoid such errors for replication configurations, avoid setting the innodb_large_prefix option on the master if it cannot also be set on the slaves, and the slaves have unique indexes that could be affected by this limit.

Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38