Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Ruby
  3. Foreigner

Foreigner

MITRubyv1.7.3

Adds foreign key helpers to ActiveRecord migrations and dumps foreign keys to schema.rb for MySQL, PostgreSQL, and SQLite.

Visit WebsiteGitHubGitHub
1.3k stars122 forks0 contributors

What is Foreigner?

Foreigner is a Ruby gem that adds foreign key support to ActiveRecord migrations for Rails applications. It provides migration helpers to create and remove foreign key constraints and ensures these constraints are properly reflected in the schema.rb file. It was designed for Rails versions before native foreign key support was introduced in Rails 4.2.

Target Audience

Rails developers working with MySQL, PostgreSQL, or SQLite databases who need to manage foreign key constraints in their migrations and schema files, particularly those using Rails versions prior to 4.2.

Value Proposition

Foreigner offers a simple, database-agnostic API for defining foreign keys in migrations, ensuring data integrity and consistent schema dumps. It integrates seamlessly with ActiveRecord's migration DSL and supports key database adapters without requiring raw SQL.

Overview

Adds foreign key helpers to migrations and correctly dumps foreign keys to schema.rb

Use Cases

Best For

  • Adding foreign key constraints to ActiveRecord migrations in Rails 3.x or 4.0/4.1 applications
  • Ensuring foreign keys are correctly dumped to schema.rb for MySQL or PostgreSQL databases
  • Managing dependent delete behavior directly on foreign key constraints
  • Using custom foreign key column names or constraint names in migrations
  • Integrating foreign key methods into create_table and change_table blocks
  • Maintaining database referential integrity in Rails projects before Rails 4.2

Not Ideal For

  • Teams using Rails 4.2 or later, as native foreign key support eliminates the need for this gem.
  • Projects relying on SQLite as their primary database, since Foreigner's methods are no-ops for SQLite.
  • Developers requiring advanced, database-specific foreign key features that aren't abstracted by Foreigner's API, such as MySQL's ON UPDATE or PostgreSQL's DEFERRABLE constraints.
  • Applications not based on ActiveRecord or those using other ORMs like Sequel or DataMapper.

Pros & Cons

Pros

Seamless Migration Integration

Adds foreign_key and remove_foreign_key methods directly to ActiveRecord migrations, enabling clean DSL usage as shown in the README examples, including integration with create_table and change_table blocks.

Accurate Schema Dumping

Ensures foreign key constraints are properly included in schema.rb for MySQL and PostgreSQL, maintaining consistency between database and schema files without manual edits.

Database-Agnostic API

Provides a unified interface for managing foreign keys across supported adapters (mysql2, postgres, sqlite), abstracting away database differences and simplifying migrations.

Dependent Option Support

Allows specifying dependent behavior like :delete directly on foreign key constraints, integrating data integrity rules into migrations rather than relying solely on model associations.

Cons

Obsolete for Modern Rails

Rendered unnecessary by Rails 4.2's native foreign key support, making it irrelevant for new projects or upgrades, and likely unmaintained or deprecated.

Limited Database Support

Only supports mysql2, postgres, and sqlite, with SQLite methods being no-ops, so it's ineffective for databases like Oracle or SQL Server without additional plugins.

No Native Advanced Features

Does not support database-specific foreign key options directly; developers must use raw SQL via the :options parameter for advanced constraints, as admitted in the README.

Frequently Asked Questions

Quick Stats

Stars1,316
Forks122
Contributors0
Open Issues2
Last commit7 years ago
CreatedSince 2009

Tags

#rails-gem#foreign-keys#activerecord#database-migrations#postgresql#mysql#schema-management#ruby-on-rails#sqlite

Built With

R
Ruby
A
ActiveRecord

Links & Resources

Website

Included in

Ruby14.1k
Auto-fetched 4 hours ago

Related Projects

PgHeroPgHero

A performance dashboard for Postgres

Stars8,894
Forks476
Last commit1 day ago
Strong MigrationsStrong Migrations

Catch unsafe migrations in development

Stars4,430
Forks199
Last commit25 days ago
ScenicScenic

Versioned database views for Rails

Stars3,624
Forks243
Last commit24 days ago
Database CleanerDatabase Cleaner

Strategies for cleaning databases in Ruby. Can be used to ensure a clean state for testing.

Stars2,962
Forks485
Last commit1 month ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub