Skip to content

perfectline/foreign_keys

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Provides foreign key support for MySQL and PostgreSQL adapters in Rails

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages