Module: MigrationConstraintHelpers
- Defined in:
- lib/migration_helpers/lib/migration_helper.rb
Instance Method Summary collapse
-
#drop_foreign_key(table, field) ⇒ Object
Drops a foreign key from
table
.field
that has been created before with foreign_key method. -
#foreign_key(table, field, referenced_table, referenced_field = :id, cascade = true) ⇒ Object
Creates a foreign key from
table
.field
against referenced_table.referenced_field. -
#primary_key(table, field) ⇒ Object
Creates a primary key for
table
, which right now HAS NOT primary key defined.
Instance Method Details
#drop_foreign_key(table, field) ⇒ Object
Drops a foreign key from table
.field
that has been created before with foreign_key method
table: The table name field: A field (or array of fields) of the table
22 23 24 |
# File 'lib/migration_helpers/lib/migration_helper.rb', line 22 def drop_foreign_key(table, field) execute "ALTER TABLE #{table} DROP FOREIGN KEY #{constraint_name(table, field)}" end |
#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 |
#primary_key(table, field) ⇒ Object
Creates a primary key for table
, which right now HAS NOT primary key defined
table: The table name field: A field (or array of fields) of the table that will be part of the primary key
30 31 32 |
# File 'lib/migration_helpers/lib/migration_helper.rb', line 30 def primary_key(table, field) execute "ALTER TABLE #{table} ADD PRIMARY KEY(#{field_list(field)})" end |