ForeignKeyChecker
This gem checks belongs_to ActiveRecord relations. It finds
- Nonpolymorphic relations without a
foreign_key - records without a record in related table (example:
city_idis 1000, but there is not city with id 1000) - broken relations: if you try to join such relation (example:
City.joins(:country)), there is an exception.
Usage
Generate migration to fix foreign key problems
bundle exec rails generate foreign_key_checker:migration
Print information about foreign key problems
bundle exec rake foreign_key_check
Or use it inside your application
ForeignKeyChecker.check.each do |key, result|
...
end
Get general information about foreign keys
ForeignKeyChecker::Utils.get_foreign_keys_hash
# => {"users"=>[#<ForeignKeyChecker::Utils::Result:0x00005645e51756e8 @from_table="user_rating_changes", @from_column="user_id", @to_table="users", @to_column="id">]}
Installation
Add this line to your application's Gemfile:
gem 'foreign_key_checker'
And then execute:
$ bundle
Or install it yourself as:
$ gem install foreign_key_checker
License
The gem is available as open source under the terms of the MIT License.