LoginSignup
8
2

More than 3 years have passed since last update.

references型にunique属性を指定する方法

Posted at

はじめに

class CreateItems < ActiveRecord::Migration[5.2]
  def change
    create_table :Items do |t|
      t.references :user, null: false, index: true, unique: true
      t.timestamps
    end
  end
end

これでmigrateを実行したが、unique属性が適応されていなかった。
解決方法をメモ程度に。

結論

class CreateItems < ActiveRecord::Migration[5.2]
  def change
    create_table :Items do |t|
      t.references :user, null: false, index: {unique: true}
      t.timestamps
    end
  end
end

で、出来ました。
DBの知識が少ないので、なぜ前者でダメなのかわかりません。調べてわかったら、追記します。

8
2
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
8
2