Search code examples
mysqlruby-on-railsdatabasereferential-integrity

Adding and removing referential integrity constraints - automatically


Is there a Rails gem, plugin or other means to automatically add or remove ALL RI constraints from your DB and schema? Or ALL of them on a per table basis? Something like:

remove_all_foreign_key_constraints('comments')


Solution

  • No gems/plugins I'm aware of.

    This is tricky though because rails (and most adapters) assume the referential integrity is maintained through your application's association.

    If that's the case (associations ARE maintained in models), then you could write a little script that would construct the required SQL statements based on the model's association.

    Association reflection might help:

    http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html#method-i-reflect_on_all_associations