public
Description: Foreign keys for Rails migrations
Homepage: www.strictlyuntyped.com
Clone URL: git://github.com/matthuhiggins/foreigner.git
name age message
file MIT-LICENSE Wed Apr 15 22:51:24 -0700 2009 First! [Matthew Higgins]
file README Sat Nov 07 11:42:29 -0800 2009 * Bump gem version. This new version includes t... [matthuhiggins]
file Rakefile Wed Apr 15 22:51:24 -0700 2009 First! [Matthew Higgins]
file foreigner.gemspec Wed Nov 11 22:17:47 -0800 2009 Releasing a new gem because postgres is screwed... [Matthew Higgins]
file init.rb Wed Apr 15 22:51:24 -0700 2009 First! [Matthew Higgins]
file install.rb Wed Apr 15 22:51:24 -0700 2009 First! [Matthew Higgins]
directory lib/ Wed Nov 11 22:21:49 -0800 2009 Fix when no database exists when using rake db:... [Matthew Higgins]
directory tasks/ Wed Apr 15 22:51:24 -0700 2009 First! [Matthew Higgins]
directory test/ Sun Sep 06 19:28:54 -0700 2009 Don't need to mess with the load path - the Rak... [Matthew Higgins]
file uninstall.rb Wed Apr 15 22:51:24 -0700 2009 First! [Matthew Higgins]
README
Foreigner
=========

Rails does not come with methods to add foreign keys. Foreigner introduces a few
methods to your migrations for adding and removing foreign key constraints.

Since each adapter implements the API, migrations using Foreigner will continue to
work on databases that do not support foreign keys, such as sqlite3.

Installation
------------

Install as a plugin:

  ruby script/plugin install git://github.com/matthuhiggins/foreigner.git

Install as a gem by adding the following to environment.rb:

  config.gem "matthuhiggins-foreigner", :lib => "foreigner", :source => http://gemcutter.org

API
---

An adapter implementing the Foreigner API implements three methods.
(Options are documented in connection_adapters/abstract/schema_definitions.rb):

  add_foreign_key(from_table, to_table, options)
  remove_foreign_key(from_table, options)
  foreign_keys(table_name)

Example
-------

The most common use of foreign keys is to reference a table that a model belongs to.
For example, given the following model:

  class Comment < ActiveRecord::Base
    belongs_to :post
  end
  
  class Post < ActiveRecord::Base
    has_many :comments, :dependent => :delete_all
  end
  
You should add a foreign key in your migration:

  add_foreign_key(:comments, :posts)

The :dependent option can be moved from the has_many definition to the foreign key:

  add_foreign_key(:comments, :posts, :dependent => :delete)

If the column is named article_id instead of post_id, use the :column option:

  add_foreign_key(:comments, :posts, :column => 'article_id')
  
Lastly, a name can be specified for the foreign key constraint:

  add_foreign_key(:comments, :posts, :name => 'comment_article_foreign_key')

Create/Change Table Shorthand
-----------------------------

Foreigner adds extra behavior to create_table and change_table, which lets you define foreign keys using shorthand.

Create the comments table with a foreign key to posts:

create_table :comments do |t|
  t.integer :post_id
  t.foreign_key :posts
end

Add a missing foreign key to comments:

change_table :comments do |t|
  t.foreign_key :posts, :dependent => :delete
end

t.foreign_key accepts the same options as add_foreign_key.


Additional t.references option
------------------------------

Foreigner extends table.references with the :foreign_key option. Pass true, and the default
foreign key options are used:

create_table :comments do |t|
  t.references :post, :foreign_key => true
end

An options hash can also be passed. It accepts the same options as add_foreign_key:

change_table :comments do |t|
  t.references :author, :foreign_key => {:dependent => :destroy}
end

By default, t.references will not generate a foreign key.

schema.rb
---------

Similar to indexes, the foreign keys in your database are automatically dumped to schema.rb.
This allows you to use foreign keys without fighting Rails!

Copyright (c) 2009 Matthew Higgins, released under the MIT license