Rails 3 Add Foreign Key in Migration Problems

∥☆過路亽.° 提交于 2019-12-11 02:23:04

问题


Does anyone know of a way to create a foreign key in Rails 3 using migrations?


回答1:


foreign_key_migrations,but I dont like that.




回答2:


the foreigner gem works well for me. it adds a few methods to Rails migrations that allow easy foreign key creation and deletion:

example:

create_table :site_credit_payments do |t|
  t.decimal  :amount, precision: 8, scale: 2, nil: false
  t.string   :note, nil: true

  t.integer  :credit_account_id

  t.timestamps
end
add_foreign_key :site_credit_payments, :credit_accounts



回答3:


If you're app has ActiveRecord::Migration (rails 3 apps do), use add_foreign_key. Documentation here: http://araddconstraint.rubyforge.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html

Note that this is a plugin and not a part of Active Record.



来源:https://stackoverflow.com/questions/3631717/rails-3-add-foreign-key-in-migration-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!