dennismonsewicz
dennismonsewicz

Reputation: 25552

Rails 3 Add Foreign Key in Migration Problems

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

Upvotes: 2

Views: 4173

Answers (3)

kdavh
kdavh

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

Archonic
Archonic

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

Ford Guo
Ford Guo

Reputation: 971

foreign_key_migrations,but I dont like that.

Upvotes: 1

Related Questions