Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@3yatsu

foreignerメモ

使い方がわからなかったのでメモ

add_foreign_key(from_table, to_table, options)

  • options = ハッシュ
key 説明
:name キー名
:column 対象カラム
:depend when :nullify then "ON DELETE SET NULL" when :delete then "ON DELETE CASCADE" when :restrict then "ON DELETE RESTRICT"
:options 自由入力っぽい

例)

def change
  add_foreing_key :table1, :table2, name: :table1_to_table2_fk
end

remove_foreign_key(table, options)

  • options = ハッシュのとき
key 外部キー名
:name 外部キー名(こっちが優先)
:column "#{table}_#{column}_fk"
  • options = ハッシュ以外 外部キーがtableとoptionsからきまる。
"#{table}_#{table.to_s.singularize}_id_fk"

例)

def change
  remove_foreing_key :table, name: :table1_to_table2_fk
end
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
3
Help us understand the problem. What are the problem?