Code Monkey home page Code Monkey logo

fakey's Introduction

Fakey - foreign keys support for ActiveRecord migrations

Before resorting to write yet another foreign key plugin I tried foreign_key_migrations (no longer available) and foreigner, but they have annoying shortcomings. foreign_key_migrations assumes every column ending on _id to be a foreign key and tries to infer the table name, which in my case was not the right one. foreigner doesn't let you declare foreign key in create_table block, instead you have to call explicitly add_foreign_key(:comments, :posts) after it. Not quite helpful for tables containing a lot of foreign keys.

Fakey is fully tested by directly inspecting schema in the database and supports both MySQL and PostgreSQL.

Example

Fakey hooks foreign keys to the standard belongs_to and references methods in migrations.

# Standard usage
create_table :books do |t|
  t.belongs_to :author
end
# => CREATE TABLE "books" ("id" serial primary key, "author_id" integer REFERENCES "authors")

# Table name specified explicitly
create_table :poems do |t|
  t.belongs_to :author, :references => :poets
end
# => CREATE TABLE "poems" ("id" serial primary key, "author_id" integer REFERENCES "poets")

# Column name specified explicitly (if you don't want _id suffix)
create_table :books do |t|
  t.belongs_to :author, :column => :author
end
# => CREATE TABLE "books" ("id" serial primary key, "author" integer REFERENCES "books")

# Non-integer primary key (for those with legacy databases) - this only works in PostgreSQL now!
execute "CREATE TABLE authors( name VARCHAR(255) PRIMARY KEY)"
create_table :books do |t|
  t.belongs_to :author_name, :column => :author_name, :references => :authors, :type => :string
end
# => CREATE TABLE "books" ("id" serial primary key, "author_name" VARCHAR(255) REFERENCES "authors")

# Non-primary key (for those with legacy databases)
create_table :authors do |t|
  t.integer :ssid
end
execute("ALTER TABLE authors ADD UNIQUE(ssid)") # note that add_index doesn't create constraint!
create_table :books do |t|
  t.belongs_to :author_ssid, :column => :author_ssid, :references => :authors, :referenced_column => :ssid
end
# => CREATE TABLE "books" ("id" serial primary key, "author_ssid" integer REFERENCES "authors"(ssid))


# Changing table too
change_table :atuhors do |t|
  t.belongs_to :author
end

Compatibility

Rails: 3.2+

PostgreSQL 7.0+

MySQL 5.0+

Copyright (c) 2009-2014 Tutuf Ltd, released under the MIT license. Thanks to Bryan Evans for database introspection query (taken from drysql gem).

fakey's People

Contributors

kanmeiban avatar

Stargazers

Fatih Yıldırım avatar

Watchers

Denitsa Belogusheva avatar James Cloos 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.