LoginSignup
1
0

More than 5 years have passed since last update.

Django: Specified key was too long; max key length is 767 bytes

Last updated at Posted at 2018-05-24

Python 3.5.2
Django 2.0

Djangoでモデルをいじっているうちに下記エラーが出るようになった。

pymysql.err.InternalError: (1071, 'Specified key was too long; max key length is 767 bytes')

なぜだかrunserverではでない。testだけで発生する。
ググってみるとMySQLのエラーのようである。
innodb_large_prefixを使ってERROR 1071を回避する

対処方法としてMySQLのデータベース変更の方法がたくさん引っかかる。

ところで、なぜrunserverでは大丈夫で、testだとだめなのか。
どうやらモデルをいじっているうちにmigrationsファイルにそういう状態が記録されてしまっているからのようだ。

だとしたら、最新のmigrationsファイルでは問題ないのだから全部まとめてしまえばよいはず。


$ python ./manage.py squashmigrations app_label 最新のmigration_name
中略
Do you wish to proceed? [yN] y
Optimizing...
  Optimized from 61 operations to 17 operations.
Created new squashed migration 
まとめられた新しいmigration_name
  You should commit this migration but leave the old ones in place;
  the new migration will be used for new installs. Once you are sure
  all instances of the codebase have applied the migrations you squashed,
  you can delete them.

古いmigrationファイルはすべて消してtestすると無事終了した。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0