Reputation: 25552
Does anyone know of a way to create a foreign key in Rails 3 using migrations?
Upvotes: 2
Views: 4173
Reputation: 414
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
Upvotes: 3
Reputation: 5382
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.
Upvotes: 0