0.0
No commit activity in last 3 years
No release in over 3 years
Foreign key support plugin for Rails, with schema dumper integration
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

Foreign Keys

Add foreign key functionality for Rails migrations and schema dumps. MySQL and PostgreSQL adapters supported.

Installation

As Rails gem (from GemCutter)

sudo gem install foreign_keys

As Rails plugin

script/plugin install git://github.com/perfectline/foreign_keys.git

Quickstart

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

add_foreign_key :albums, :user_id

The referenced table and column are decidec by using rails conventions, i.e this example would result in a foreign key from 'albums.user_id' to 'users.id' column.

You can additionally provide multiple options for cases where the association information cannot be decided by conventions.

add_foreign_key :albums, :author_id, :references => :users, :keys => :user_id, :name => "my_special_fk"

Removing foreign keys is just as easy. You can remove them either by providing the constraint name or column(s).

remove_foreign_key :albums, :user_id
remove_foreign_key :albums, :name => "my_special_fk"

Note: MySQL creates an index on the FK column automatically. Removing a FK will remove that index also.

Options

  • references: name of the references table
  • keys: column name(s) on the references table
  • on_delete: on delete hook with a value of :restrict, :set_null, :cascade
  • on_update: on update hook with a value of :restrict, :set_null, :cascade
  • name: foreign key constraint name

Schema Dump

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

Authors

Tanel Suurhans - tanel.suurhans_at_perfectline.ee Tarmo Lehtpuu - tarmo.lehtpuu_at_perfectline.ee

This gem is based ideas and enhanchements from Dan Walters "foreign_keys" plugin-

License

Copyright 2009 by PerfectLine LLC (http://www.perfectline.co.uk) and is released under the MIT license.