public
Fork of vigetlabs/foreign_key_migrations
Description: A gem/plugin for ActiveRecord that lets you define foreign keys in migrations.
Clone URL: git://github.com/crnixon/foreign_key_migrations.git
crnixon (author)
Sun Jul 20 10:34:18 -0700 2008
commit  9afb492e862c936d04f0507d5b82ef96b9a02260
tree    1c386cfdf3cfa1756ebf980ca815ef1aafe51d6b
parent  1796cc7672e9c5c6a489aad374e9eb2204446720
name age message
file .gitignore Wed Jun 25 14:51:33 -0700 2008 Ignore pkg directory [reagent]
file CHANGELOG Thu Aug 09 08:27:15 -0700 2007 r252@handcar: crnixon | 2007-08-09 11:10:48 -... [cnixon]
file History.txt Mon Jul 14 20:21:28 -0700 2008 moved to Mr Bones structure [crnixon]
file Manifest.txt Sun Jul 20 10:34:18 -0700 2008 moves tasks dir to prevent plugin collision [crnixon]
file README.rdoc Tue Jul 15 12:34:12 -0700 2008 fixed rdoc [crnixon]
file Rakefile Sun Jul 20 10:34:18 -0700 2008 moves tasks dir to prevent plugin collision [crnixon]
directory bones_tasks/ Sun Jul 20 10:34:18 -0700 2008 moves tasks dir to prevent plugin collision [crnixon]
file init.rb Thu Aug 09 08:27:15 -0700 2007 r252@handcar: crnixon | 2007-08-09 11:10:48 -... [cnixon]
directory integration_tests/ Fri Feb 08 13:48:32 -0800 2008 added test for symbols [cnixon]
directory lib/ Sun Jul 20 10:34:18 -0700 2008 moves tasks dir to prevent plugin collision [crnixon]
directory test/ Tue Jul 15 12:27:50 -0700 2008 added ability to create foreign keys with .refe... [crnixon]
README.rdoc

foreign_key_migrations plugin for ActiveRecord

by Clinton R. Nixon of Viget Labs

URL: http://github.com/crnixon/foreign_key_migrations

DESCRIPTION:

Allows foreign keys to be specified in ActiveRecord migrations. Foreign keys via this plugin are currently supported for the following database adapters:

  • MySQL
  • PostgreSQL
  • SQLite
  • SQLite3

Foreign keys can be specified in four ways, modeled on indexes and current ActiveRecord migration syntax.

  • In a column definition:
      class AddUsersTable < ActiveRecord::Migration
    
        def self.up
          create_table :users do |t|
            t.column :department_id, :integer, :references => :departments
          end
        end
    
      end
    
  • In a column definition, using references or belongs_to:
      class AddUsersTable < ActiveRecord::Migration
    
        def self.up
          create_table :users do |t|
            t.references :department
            # t.belongs_to :department also works
          end
        end
    
      end
    
  • In a table definition:
      class AddUsersTable < ActiveRecord::Migration
    
        def self.up
          create_table :users do |t|
            t.column :department_id, :integer
            t.foreign_key :on => :department_id, :references => :departments
          end
        end
    
      end
    
  • Or outside a table definition:
      class AddUsersTable < ActiveRecord::Migration
    
        def self.up
          create_table :users do |t|
            t.column :department_id, :integer
          end
    
          add_foreign_key :on => :users, :references => :departments
        end
    
      end
    

Foreign keys can be more complex to deal with non-Rails or legacy databases, such as:

    add_foreign_key
        :on => {:table => :users, :column => :dept_id},
        :references => {:table => :departments, :column => :dept_id}

FEATURES/PROBLEMS:

  • No integration tests for SQLite or PostgreSQL.

REQUIREMENTS:

  • To run the tests, you need the Shoulda, spect, and mocha gems.
  • ActiveRecord

LICENSE:

(ISC License)

Copyright © 2008 Clinton R. Nixon of Viget Labs

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.