2
0

More than 3 years have passed since last update.

Rails で外部キー制約をつける際に別名をつける

Posted at
class Test < ActiveRecord::Migration[5.2]
  def change
    add_reference :tags, :owner, foreign_key: { to_table: :users }, default: nil, null: true
  end
end
class Test < ActiveRecord::Migration[5.2]
  def up
    add_reference :tags, :owner, foreign_key: { to_table: :users }, default: nil, null: true
  end

  def down
    remove_reference :tags, :owner, foreign_key: { to_table: :users }, default: nil, null: true
  end
end

add_reference :tags, :owner と指定した場合、カラム名は owner_id となる。

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