使い方がわからなかったのでメモ
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