{"id":13483765,"url":"https://github.com/matthuhiggins/foreigner","last_synced_at":"2025-05-14T13:09:39.375Z","repository":{"id":547238,"uuid":"177321","full_name":"matthuhiggins/foreigner","owner":"matthuhiggins","description":"Adds foreign key helpers to migrations and correctly dumps foreign keys to schema.rb","archived":false,"fork":false,"pushed_at":"2019-02-06T16:22:54.000Z","size":395,"stargazers_count":1324,"open_issues_count":4,"forks_count":123,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-04-11T06:12:52.663Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"www.strictlyuntyped.com","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/matthuhiggins.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2009-04-16T05:50:41.000Z","updated_at":"2025-02-07T17:01:22.000Z","dependencies_parsed_at":"2022-08-06T09:15:44.885Z","dependency_job_id":null,"html_url":"https://github.com/matthuhiggins/foreigner","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matthuhiggins%2Fforeigner","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matthuhiggins%2Fforeigner/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matthuhiggins%2Fforeigner/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matthuhiggins%2Fforeigner/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matthuhiggins","download_url":"https://codeload.github.com/matthuhiggins/foreigner/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253589921,"owners_count":21932563,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:01:15.067Z","updated_at":"2025-05-14T13:09:34.335Z","avatar_url":"https://github.com/matthuhiggins.png","language":"Ruby","readme":"# Foreigner\n[![Build Status](https://travis-ci.org/matthuhiggins/foreigner.svg)](https://travis-ci.org/matthuhiggins/foreigner) [![Code Climate](https://codeclimate.com/github/matthuhiggins/foreigner.svg)](https://codeclimate.com/github/matthuhiggins/foreigner)\n\nForeigner introduces a few methods to your migrations for adding and removing foreign key constraints. It also dumps foreign keys to `schema.rb`.\n\nThe following adapters are supported:\n\n* mysql2\n* postgres\n* sqlite (foreign key methods are a no-op)\n\n**Foreigner was rendered obsolete in Rails 4.2**. The migration DSL [supports foreign keys out of the box](http://edgeguides.rubyonrails.org/4_2_release_notes.html#foreign-key-support) via `add_foreign_key` and `remove_foreign_key`.\n\n## Installation\n\nAdd the following to your Gemfile:\n```ruby\ngem 'foreigner'\n```\n## API Examples\n\nForeigner adds two methods to migrations.\n\n* `add_foreign_key(from_table, to_table, options)`\n* `remove_foreign_key(from_table, to_table, options)`\n\n(Options are documented in `connection_adapters/abstract/schema_statements.rb`):\n\nFor example, given the following model:\n```ruby\nclass Comment \u003c ActiveRecord::Base\n  belongs_to :post\nend\n\nclass Post \u003c ActiveRecord::Base\n  has_many :comments, dependent: :delete_all\nend\n```  \nYou should add a foreign key in your migration:\n```ruby\nadd_foreign_key(:comments, :posts)\n```\nThe `:dependent` option can be moved from the `has_many` definition to the foreign key:\n```ruby\nadd_foreign_key(:comments, :posts, dependent: :delete)\n```\nIf the column is named `article_id` instead of `post_id`, use the `:column` option:\n```ruby\nadd_foreign_key(:comments, :posts, column: 'article_id')\n```\nA name can be specified for the foreign key constraint:\n```ruby\nadd_foreign_key(:comments, :posts, name: 'comment_article_foreign_key')\n```\nThe `:column` and `:name` options create a foreign key with a custom name. In order to remove it you need to specify `:name`:\n```ruby\nremove_foreign_key(:comments, name: 'comment_article_foreign_key')\n```\n## Change Table Methods\n\nForeigner adds extra methods to `create_table` and `change_table`.\n\nCreate a new table with a foreign key:\n```ruby\ncreate_table :products do |t|\n  t.string :name\n  t.integer :factory_id\n  t.foreign_key :factories\nend\n```\nAdd a missing foreign key to comments:\n```ruby\nchange_table :comments do |t|\n  t.foreign_key :posts, dependent: :delete\nend\n```\nRemove an unwanted foreign key:\n```ruby\nchange_table :comments do |t|\n  t.remove_foreign_key :users\nend\n```\n## Database-specific options\n\nDatabase-specific options will never be supported by foreigner. You can add them using `:options`:\n```ruby\nadd_foreign_key(:comments, :posts, options: 'ON UPDATE DEFERRED')\n```\n## Foreigner Add-ons\n\n* [immigrant](https://github.com/jenseng/immigrant) - generate a migration that includes all missing foreign keys.\n* [sqlserver-foreigner](https://github.com/cleblanc87/sqlserver-foreigner) - A plugin for SQL Server.\n\n## License\n\nCopyright (c) 2012 Matthew Higgins, released under the MIT license\n","funding_links":[],"categories":["Ruby","Database Tools"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatthuhiggins%2Fforeigner","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatthuhiggins%2Fforeigner","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatthuhiggins%2Fforeigner/lists"}