Does anyone know of a way to create a foreign key in Rails 3 using migrations?
Asked
Active
Viewed 4,159 times
3 Answers
3
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

kdavh
- 404
- 6
- 13
0
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.

Archonic
- 5,207
- 5
- 39
- 55
-
This isn't part of AR it's a separate plugin. – PhilT Aug 02 '13 at 09:14
-
I never said it was a part of AR. – Archonic Aug 02 '13 at 13:25