Created: 2011-07-11 11:49
Updated: 2015-11-05 10:09


Foreign Keys Plugin

This plugin adds basic support for foreign keys, by providing some additional schema statements as well as integration into the standard schema dumper.

Currently, mysql and postgresql connection adapters are supported.


Via standard git clone:

git clone git:// vendor/plugins/foreign_keys

Via rails plugin (requires 2.1):

script/plugin install git://

Adding Foreign Keys

Just place the appropriate statements in your migrations. The simplest example is:

add_foreign_key :orders, :user_id

The usual rails conventions are used to deduce that this should result in a foreign key from the orders.user_id column to the column

Of course, you may specify these values explicitly too, and will need to when the conventions don't match exactly:

add_foreign_key :articles, :author_id, :references => :users

Some other options of note:

  • references: the name of the referenced table
  • keys: the corresponding column name(s) in the referenced table
  • on_delete / on_update: one of :restrict, :set_null, :cascade
  • name: the name to assign to the foreign key constraint

Note that recent mysql versions will create an index on the referenced column(s) if one does not already exist.

You may wish to check the documentation for further options.

Removing Foreign Keys

You can remove foreign keys by name:

remove_foreign_key :orders, :name => "index_orders_on_user_id"

Or the name can be deduced if you used standard conventions when you created it:

remove_foreign_key :orders, :user_id

This will not automatically remove any indexes.

Schema Dump

The usual rake targets for db:schema:dump, db:schema:load, and db:reset should all work as desired.


Dan Walters

Max Lapshin -- PostgreSQL support

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more