最大密钥长度为767字节

最大密钥长度为767字节

本文介绍了django.db.utils.OperationalError:(1071,'指定的密钥太长;最大密钥长度为767字节')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模特:

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

我要添加"unique_together":

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

迁移时出现错误:

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

我的数据库是: mysql Ver 15.1 Distrib 10.0.17-MariaDB,用于使用readline 5.2的debian-linux-gnu(x86_64)

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

我试图增加索引mysql的长度:

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)

但这还不够:

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

问题索引长度.如何指定Django索引的长度限制?

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

推荐答案

解决方案是

ALTER DATABASE `databasename` CHARACTER SET utf8;

这篇关于django.db.utils.OperationalError:(1071,'指定的密钥太长;最大密钥长度为767字节')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 18:32