Question

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

Was it helpful?

Solution

foreign_key_migrations,but I dont like that.

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top