LoginSignup
0
0

text型のindex設定時は長さを指定する

Posted at

前提

  • 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
0
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
0
0