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

前端 未结 7 635
故里飘歌
故里飘歌 2020-12-15 09:03

My model:

class Course(models.Model):
    language = models.ForeignKey(Language)
    name = models.CharField(max_length=50, unique=True, default=\'course\')
         


        
相关标签:
7条回答
  • 2020-12-15 09:26

    I ended up adding

    'OPTIONS': { 'init_command': 'SET storage_engine=INNODB;' }
    

    to my DB backed configuration in settings.py and that fixed the problem.

    The MySQL server was configured to use InnoDB as a default engine, but due to some reason it still tried to create tables with the MyISAM. I am running MySQL 5.1 with Django 2.2.1 and Python 3.6.7

    0 讨论(0)
  • 2020-12-15 09:29

    The previous two answers did not help in my case, so I'm posting my solution to my case when your limit is 1000 (i.e. 1071, 'Specified key was too long; max key length is 1000 bytes').

    First of all, make sure you are working on utf8 encoding!

    Then, navigate to your setting file my.ini, find the line default-storage-engine=xxx. If it is

    default-storage-engine=MYISAM
    

    please change to

    default-storage-engine=InnoDB
    

    Then, the problem should be solved.

    The reason is simply because MYISAM does not support key size greater than 1000 bytes.

    0 讨论(0)
  • 2020-12-15 09:36

    You can recreate "database" again:

    CREATE DATABASE mydatabase CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
    

    or change config file. Open /etc/mysql/mariadb.conf.d/50-server.cnf and change:

    character-set-server  = utf8
    collation-server      = utf8_general_ci
    
    0 讨论(0)
  • 2020-12-15 09:37

    Just upgrade and migrate database to MySQL-8.0.11 or ** MySQL-5.7.21** also make sure to use utf8 and utf8_general_ci

    I had that problem, then I updated to 8.0.11 on my testing enviroment and 5.7.21 on the server. Now it migrates on both enviroments.

    0 讨论(0)
  • 2020-12-15 09:42

    solution is

    ALTER DATABASE `databasename` CHARACTER SET utf8; 
    
    0 讨论(0)
  • 2020-12-15 09:43

    Upgrade mysql to 5.7 and try migrate again.

    wget https://dev.mysql.com/get/mysql-apt-config_0.8.1-1_all.deb  
    sudo dpkg -i mysql-apt-config_0.8.1-1_all.deb  
    sudo apt-get update
    sudo apt-get install mysql-server
    
    0 讨论(0)
提交回复
热议问题