Referential Integrity for ActiveRecord

Adds as reversible add_foreign_key migration to Rails. add_foreign_key creates a database level foreign key constraint, this ensures that records cannot reference foreign keys in other tables that do not exist.

See PostgreSQL foreign key constraint documentation for more.

Limitations

Currently supports only PostgreSQL. Currently is written against Rails/ActiveRecord 3.2.16.

Installation

Add this line to your application's Gemfile:

gem 'activerecord-referential_integrity'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-referential_integrity

Usage

Inside your migrations, call add_foreign_key, passing in the table and a hash of column => table pairs.

For example:

class CreateUserSettings < ActiveRecord::Migration
  def change
    create_table :user_settings do |t|
      t.references :user, null: false
      # or t.integer :user_id, null: false
      [...]
    end

    add_foreign_key :somethings, :user_id => :users
  end
end

TODO: Write usage instructions here

Versioning

Uses Semantic Versioning.

Contributing

  1. Fork it (http://github.com/orcasnet/activerecord-referential_integrity/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request