Method: MigrationConstraintHelpers#foreign_key

Defined in:
lib/migration_helpers/lib/migration_helper.rb

#foreign_key(table, field, referenced_table, referenced_field = :id, cascade = true) ⇒ Object

Creates a foreign key from table.field against referenced_table.referenced_field

table: The tablename field: A field of the table referenced_table: The table which contains the field referenced referenced_field: The field (which should be part of the primary key) of the referenced table cascade: delete & update on cascade?



10
11
12
13
14
15
# File 'lib/migration_helpers/lib/migration_helper.rb', line 10

def foreign_key(table, field, referenced_table, referenced_field = :id, cascade = true)
   execute "ALTER TABLE #{table} ADD CONSTRAINT #{constraint_name(table, field)}
            FOREIGN KEY #{constraint_name(table, field)} (#{field_list(field)})
            REFERENCES #{referenced_table}(#{field_list(referenced_field)})
            #{(cascade ? 'ON DELETE CASCADE ON UPDATE CASCADE' : '')}"
end