Skip to content

Instantly share code, notes, and snippets.

@pragdave
Created January 27, 2011 20:21
Show Gist options
  • Save pragdave/799172 to your computer and use it in GitHub Desktop.
Save pragdave/799172 to your computer and use it in GitHub Desktop.
module MigrationHelpers
def fk(from_table, from_column, to_table=from_column.to_s.sub(/_id$/, 's'))
constraint_name = "fk_#{from_table}_#{from_column}"
execute %{alter table #{from_table}
add constraint #{constraint_name}
foreign key (#{from_column})
references #{to_table}(id)}
end
def drop_fk(from_table, from_column)
constraint_name = "fk_#{from_table}_#{from_column}"
execute %{alter table #{from_table} drop foreign key #{constraint_name}}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment