Description
Foreigner introduces a few methods to your migrations for adding and removing foreign key constraints. It also dumps foreign keys to schema.rb.
The following adapters are supported:
Foreigner alternatives and similar gems
Based on the "Database Tools" category.
Alternatively, view Foreigner alternatives based on common mentions on social networks and blogs.
-
Database Cleaner
Strategies for cleaning databases in Ruby. Can be used to ensure a clean state for testing. -
Lol DBA
lol_dba is a small package of rake tasks that scan your application models and displays a list of columns that probably should be indexed. Also, it can generate .sql migration scripts. -
Seed Fu
Advanced seed data handling for Rails, combining the best practices of several methods together. -
Seedbank
Seedbank gives your seed data a little structure. Create seeds for each environment, share seeds between environments and specify dependencies to load your seeds in order. All nicely integrated with simple rake tasks. -
DatabaseConsistency
The tool to avoid various issues due to inconsistencies and inefficiencies between a database schema and application models. -
Polo
Polo travels through your database and creates sample snapshots so you can work with real world data in development. -
SchemaPlus
DISCONTINUED. SchemaPlus provides a collection of enhancements and extensions to ActiveRecord -
Upsert
Upsert on MySQL, PostgreSQL, and SQLite3. Transparently creates functions (UDF) for MySQL and PostgreSQL; on SQLite3, uses INSERT OR IGNORE. -
OnlineMigrations
Catch unsafe PostgreSQL migrations in development and run them easier in production (code helpers for table/column renaming, changing column type, adding columns with default, background migrations, etc). -
Ruby PG Extras
Ruby PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more. -
PgDriveBackup
Simple solution to make encrypted with ccrypt PostgreSQL backups and storing on Google Drive API -
Slack Smart Bot
Create a Slack bot that is smart and so easy to expand, create new bots on demand, run ruby code on chat, create shortcuts... The main scope of this gem is to be used internally in the company so teams can create team channels with their own bot to help them on their daily work, almost everything is suitable to be automated!! slack-smart-bot can create bots on demand, create shortcuts, run ruby code... just on a chat channel. You can access it just from your mobile phone if you want and run those tests you forgot to run, get the results, restart a server... no limits. -
Perfect Shape
Perfect Shape is a collection of geometric algorithms that are mostly useful for GUI manipulation like checking containment of a point in popular geometric shapes such as rectangle, square, arc, circle, polygon, and paths containing lines, quadratic bézier curves, and cubic bezier curves. Also, some general math algorithms like IEEE-754 Remainder. -
ActiveRecord Setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll). -
Rapidity
Simple but fast Redis-backed distributed rate limiter. Allows you to specify time interval and count within to limit distributed operations. -
PRY-BYETYPO 👋
A Pry plugin that captures exceptions that may arise from typos and deduces the correct command.
CodeRabbit: AI Code Reviews for Developers

* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Foreigner or a related project?
README
Foreigner
Foreigner introduces a few methods to your migrations for adding and removing foreign key constraints. It also dumps foreign keys to schema.rb
.
The following adapters are supported:
- mysql2
- postgres
- sqlite (foreign key methods are a no-op)
Foreigner was rendered obsolete in Rails 4.2. The migration DSL supports foreign keys out of the box via add_foreign_key
and remove_foreign_key
.
Installation
Add the following to your Gemfile:
gem 'foreigner'
API Examples
Foreigner adds two methods to migrations.
add_foreign_key(from_table, to_table, options)
remove_foreign_key(from_table, to_table, options)
(Options are documented in connection_adapters/abstract/schema_statements.rb
):
For example, given the following model:
class Comment < ActiveRecord::Base
belongs_to :post
end
class Post < ActiveRecord::Base
has_many :comments, dependent: :delete_all
end
You should add a foreign key in your migration:
add_foreign_key(:comments, :posts)
The :dependent
option can be moved from the has_many
definition to the foreign key:
add_foreign_key(:comments, :posts, dependent: :delete)
If the column is named article_id
instead of post_id
, use the :column
option:
add_foreign_key(:comments, :posts, column: 'article_id')
A name can be specified for the foreign key constraint:
add_foreign_key(:comments, :posts, name: 'comment_article_foreign_key')
The :column
and :name
options create a foreign key with a custom name. In order to remove it you need to specify :name
:
remove_foreign_key(:comments, name: 'comment_article_foreign_key')
Change Table Methods
Foreigner adds extra methods to create_table
and change_table
.
Create a new table with a foreign key:
create_table :products do |t|
t.string :name
t.integer :factory_id
t.foreign_key :factories
end
Add a missing foreign key to comments:
change_table :comments do |t|
t.foreign_key :posts, dependent: :delete
end
Remove an unwanted foreign key:
change_table :comments do |t|
t.remove_foreign_key :users
end
Database-specific options
Database-specific options will never be supported by foreigner. You can add them using :options
:
add_foreign_key(:comments, :posts, options: 'ON UPDATE DEFERRED')
Foreigner Add-ons
- immigrant - generate a migration that includes all missing foreign keys.
- sqlserver-foreigner - A plugin for SQL Server.
License
Copyright (c) 2012 Matthew Higgins, released under the MIT license
*Note that all licence references and agreements mentioned in the Foreigner README section above
are relevant to that project's source code only.