Rails gem / plugin which generates mysql foreign key constraints
Ruby
Pull request Compare This branch is 4 commits ahead, 4 commits behind sumskyi:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
rails
test
.gitignore
Gemfile
README.rdoc
Rakefile
VERSION
artemk-migration_fu-0.0.1.gem
artemk-migration_fu.gemspec
init.rb
install.rb

README.rdoc

MigrationFu

Rails gem / plugin for generating mysql foreign key constraints.

Install

as gem

sudo gem install artemk-migration_fu

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