lib/foreign_key_migrations.rb
# Attempts to add a foreign key using ANSI SQL Standard syntax, so most database
#   providers should work just fine.
#
# ===Params
# * table_name - the name of the database table being altered
# * foreign_key - name of foreign key column or an array of fk column names
# * references - name of primary key column or an array of pk column names
# * options - hash of additional options accepted options: * :name - name of
#   constraint * :on_update - referential action to take (CASCADE, RESTRICT, NO
#   ACTION, SET NULL, SET DEFAULT) * :on_delete - referential action to take
#   (CASCADE, RESTRICT, NO ACTION, SET NULL, SET DEFAULT)
Attempts to add a foreign key using ANSI SQL Standard syntax, so most database
providers should work just fine.


Attempts to add a foreign key using ANSI SQL Standard syntax so most database providers should work just fine .
def add_foreign_key(table_name, foreign_key, references, options={})
        prep_args(table_name, foreign_key, references, options) do |t, f, r, o|
          table_name = t
          foreign_key = f
          references = r
          options = o
        end
        begin
          query = "ALTER TABLE #{table_name} "
          constraint_name = get_constraint_name(table_name, foreign_key, references, options)
          query << "ADD CONSTRAINT #{constraint_name} "
          query << "FOREIGN KEY #{constraint_name} ("
          if foreign_key.is_a?(Array)
            query << "#{foreign_key.join(",")}"
          else
            query << "#{foreign_key}"
          end
          query << ") REFERENCES #{references} (id)"
          query << "ON UPDATE #{options[:on_update]} " if options[:on_update]
          query << "ON DELETE #{options[:on_delete]} " if options[:on_delete]
          execute(query)
        rescue
          query = "ALTER TABLE #{table_name} "
          constraint_name = get_constraint_name(table_name, foreign_key, references, options)
          query << "ADD CONSTRAINT #{constraint_name} "
          query << "FOREIGN KEY ("
          if foreign_key.is_a?(Array)
            query << "#{foreign_key.join(",")}"
          else
            query << "#{foreign_key}"
          end
          query << ") REFERENCES #{references} (id)"
          query << "ON UPDATE #{options[:on_update]} " if options[:on_update]
          query << "ON DELETE #{options[:on_delete]} " if options[:on_delete]
          execute(query)
        end
      rescue
        if respond_to?(:puts_migration_error)
          puts_migration_error($!)
        else
          raise
        end
      end
def add_foreign_key ( table_name , foreign_key , references , options = { } ) prep_args ( table_name , foreign_key , references , options ) do | t , f , r , o | table_name = t foreign_key = f references = r options = o end begin query = "ALTER TABLE #{table_name} " constraint_name = get_constraint_name ( table_name , foreign_key , references , options ) query << "ADD CONSTRAINT #{constraint_name} " query << "FOREIGN KEY #{constraint_name} (" if foreign_key . is_a? ( Array ) query << "#{foreign_key.join(",")}" else query << "#{foreign_key}" end query << ") REFERENCES #{references} (id)" query << "ON UPDATE #{options[:on_update]} " if options [ :on_update ] query << "ON DELETE #{options[:on_delete]} " if options [ :on_delete ] execute ( query ) rescue query = "ALTER TABLE #{table_name} " constraint_name = get_constraint_name ( table_name , foreign_key , references , options ) query << "ADD CONSTRAINT #{constraint_name} " query << "FOREIGN KEY (" if foreign_key . is_a? ( Array ) query << "#{foreign_key.join(",")}" else query << "#{foreign_key}" end query << ") REFERENCES #{references} (id)" query << "ON UPDATE #{options[:on_update]} " if options [ :on_update ] query << "ON DELETE #{options[:on_delete]} " if options [ :on_delete ] execute ( query ) end rescue if respond_to? ( :puts_migration_error ) puts_migration_error ( $! ) else raise end end
Attempts to add a foreign key using ANSI SQL Standard syntax, so most database
providers should work just fine.
Attempts to add a foreign key using ANSI SQL Standard syntax so most database providers should work just fine .
table_name
<not_specific>
foreign_key
<not_specific>
references
<not_specific>
options
<not_specific>