20
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

migration時のreferencesで作成されるindex nameを変更したい [Rails]

Posted at

経緯

Railsで外部キー制約をつけるために以下のようなmigrationコードを組んだが


create_table :all_you_can_drink_course_shops do |t|
  t.references :all_you_can_drink_course, null: false, foreign_key: true
  t.references :shop,                     null: false, foreign_key: true
  t.timestamps
end

自動生成されるindexの名前が長すぎて弾かれる

Index name 'index_all_you_can_drink_course_shops_on_all_you_can_drink_course_id' on table 'all_you_can_drink_course_shops' is too long; the limit is 63 characters

解決作

indexのパラメータにネストする形で、index の名前を帰れるようです

create_table :all_you_can_drink_course_shops do |t|
  t.references :all_you_can_drink_course, index: { name: 'idx_a_y_c_d_course_shops_on_all_you_can_drink_course_id' }, null: false, foreign_key: true
  t.references :shop,                     null: false, foreign_key: true
  t.timestamps
end

参考
migrations: t.references doesn't allow index name to be specified

20
6
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
20
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?