百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
I

immigrant

> 编程语言
开源

Rails 外键迁移生成器

496 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Rails 外键迁移生成器

Immigrant

Immigrant gives Rails a foreign key migration generator so you can effortlessly find and add missing keys. This is particularly helpful when you decide to add keys to an established Rails app.

Installation

Add the following to your Gemfile:

ruby
gem 'immigrant'

If you're using a version of Rails prior to 4.2, you'll also need the Foreigner gem.

Usage

bash
rails generate immigration AddKeys

This will create a migration named AddKeys which will have add_foreign_key statements for any missing foreign keys. Immigrant infers missing ones by evaluating the associations in your models (e.g. belongs_to, has_many, etc.). Only missing keys will be added; existing ones will never be altered or removed.

Rake Task

To help you remember to add keys in the future, there's a handy rake task you can add to your CI setup. Just run rake immigrant:check_keys, and if anything is missing it will tell you about it and exit with a non-zero status.

Skipping associations

Immigrant.ignore_keys allows you to specify a list of keys that should be ignored (both in the migration generator and the rake task). This is useful if you have associations spanning databases.

Just create an config/initializers/immigrant.rb file with something like the following:

ruby
Immigrant.ignore_keys = [
  { from_table: "users", column: "account_id" },
  # etc
]

Considerations

If the data in your tables is bad, then the migration will fail to run (obviously). IOW, ensure you don't have orphaned records before you try to add foreign keys.

Known Issues

Immigrant currently only looks for foreign keys in ActiveRecord::Base's database. So if a model is using a different database connection and it has foreign keys, Immigrant will incorrectly include them again in the generated migration. Immigrant.ignore_keys can be used to work around this.

Changelog

License

Copyright (c) 2012-2015 Jon Jensen, released under the MIT license

Issues· 6 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言