Referential Integrity for ActiveRecord
Adds as reversible add_referential_integrity
migration to Rails. add_referential_integrity
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_referential_integrity
, 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_referential_integrity :user_settings, user_id: :users
end
end
TODO: Write usage instructions here
Versioning
Uses Semantic Versioning.
Contributing
- Fork it (http://github.com/orcasnet/activerecord-referential_integrity/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request