Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
a simple rails gem / plugin to generate mysql foreign key constraints
Ruby
branch: master

format

latest commit 973cf2aa3a
@sleistner authored
Failed to load latest commit information.
lib format
test format
.gitignore gem abilities added
README.rdoc format
Rakefile format
init.rb gem abilities added
install.rb IO puts README path changed

README.rdoc

MigrationFu

Rails gem / plugin for generating mysql foreign key constraints.

Install

as gem

sudo gem install sleistner-migration_fu --source http://gems.github.com

or plugin

script/plugin install git://github.com/sleistner/migration_fu.git

Usage

-----------------       -----------------
|    users      |       |   addresses   |
-----------------       -----------------
| id            |       | id            |
| username      | <---- | user_id       |
| password      |       | street        |
-----------------       -----------------

[ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
[ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]

see http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html

arguments:  from_table,  to_table,  options => { :name, :on_delete, :on_update }

add_foreign_key(:addresses, :users, :name => 'fk_add_user')
add_foreign_key(:addresses, :users, :on_delete => :cascade)
add_foreign_key(:addresses, :users,  :on_delete => :cascade, :on_update => :cascade)

class CreateUsers < ActiveRecord::Migration

  def self.up
    create_table :users, :force => true do |t|
      t.string :username, :null => false
      t.string :password, :null => false
    end

    create_table :addresses, :force => true do |t|
      t.references :user
      t.string :street, :null => false
    end

    add_foreign_key(:addresses, :users, :on_delete => :cascade)
  end
end
Something went wrong with that request. Please try again.