当前位置: 动力学知识库 > 问答 > 编程问答 >

python - django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')

问题描述:

My model:

class Course(models.Model):

language = models.ForeignKey(Language)

name = models.CharField(max_length=50, unique=True, default='course')

title = models.CharField(max_length=1024, default='no title')

foreign_title = models.CharField(max_length=1024, default='no title', blank=True)

header = models.CharField(max_length=1024, default='', blank=True)

description = models.TextField(null=True, blank=True)

def __str__(self):

return self.title

def __unicode__(self):

return u'%s' % self.title

I am add "unique_together":

class Course(models.Model):

language = models.ForeignKey(Language)

name = models.CharField(max_length=50, unique=True, default='course')

title = models.CharField(max_length=1024, default='no title')

foreign_title = models.CharField(max_length=1024, default='no title', blank=True)

header = models.CharField(max_length=1024, default='', blank=True)

description = models.TextField(null=True, blank=True)

class Meta:

unique_together = ['language', 'name', 'title']

def __str__(self):

return self.title

def __unicode__(self):

return u'%s' % self.title

In-migration time getting the error:

 django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')

My DB is:

mysql Ver 15.1 Distrib 10.0.17-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

I tried to increase the length of the index mysql:

MariaDB [(none)]> set global innodb_file_format = BARRACUDA;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> set global innodb_large_prefix = ON;

Query OK, 0 rows affected (0.00 sec)

But this is still not enough:

django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 3072 bytes')

The problem index length. How to specify the length limit Django index?

网友答案:

I think all 4 of these things are needed:

SET GLOBAL innodb_file_per_table = ON,
           innodb_file_format = Barracuda,
           innodb_large_prefix = ON;
CREATE/ALTER TABLE ...
    ROW_FORMAT = DYNAMIC or COMPRESSED

This should get past the limit of 767 bytes for one column, but won't get past the 3072 bytes limit for the entire index.

In order to have a compound unique index composed of strings, normalize some of the strings. Replacing a long string with a 4-byte INT will shrink the index below the limit.

分享给朋友:
您可能感兴趣的文章:
随机阅读: