public
Description: A gem/plugin for ActiveRecord that lets you define foreign keys in migrations.
Clone URL: git://github.com/vigetlabs/foreign_key_migrations.git
foreign_key_migrations / README.rdoc
100644 104 lines (69 sloc) 2.765 kb

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.