Giter VIP home page Giter VIP logo

foreigner's Introduction

Foreigner

<img src=“https://secure.travis-ci.org/matthuhiggins/foreigner.png?rvm=1.9.3” />

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)

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, options)

(Options are documented in connection_adapters/abstract/schema_definitions.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')

Change Table Methods

Foreigner adds extra methods to change_table.

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

Finding Missing Foreign Keys

Use Immigrant to generate a migration that includes all missing foreign keys.

License

Copyright © 2012 Matthew Higgins, released under the MIT license

foreigner's People

Contributors

aschrab avatar chipiga avatar ctm avatar hms avatar ihid avatar juanmcuello avatar matthuhiggins avatar sobrinho avatar yellow5 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.