前提
- Rails 7.0.4
- MySQL8系(docker)
エラー内容
text型のindex設定でマイグレーションしたら
add_index :table_name, [:not_text_column, :text_column]
add_index :table_name, :text_column
エラーが起きた
...
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: BLOB/TEXT column 'normalized_value' used in key specification without a key length
...
Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: BLOB/TEXT column 'normalized_value' used in key specification without a key length
...
Caused by:
Mysql2::Error: BLOB/TEXT column 'normalized_value' used in key specification without a key length
対応
こちらを参考にすると、MySQLでは文字列カラムのインデックス長が制限されているとのこと
Railsドキュメントを参考に、インデックス長を指定すればOK
add_index :table_name, [:not_text_column, :text_column], length: {text_column: 10}
add_index :table_name, :text_column, length: 10