Guest User

Untitled

a guest
Jul 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. module MigrationHelpers
  2.  
  3. def fk(from_table, from_column, to_table=from_column.to_s.sub(/_id$/, 's'))
  4. constraint_name = "fk_#{from_table}_#{from_column}"
  5.  
  6. execute %{alter table #{from_table}
  7. add constraint #{constraint_name}
  8. foreign key (#{from_column})
  9. references #{to_table}(id)}
  10. end
  11.  
  12. def drop_fk(from_table, from_column)
  13. constraint_name = "fk_#{from_table}_#{from_column}"
  14. execute %{alter table #{from_table} drop foreign key #{constraint_name}}
  15. end
  16.  
  17. end
Add Comment
Please, Sign In to add comment