Feb 5, 2009
Check Constraints and MySQL
Unfortunately, MySQL does not support check constraints out of the box. This makes the task of enforcing business logic in the database layer difficult, but not impossible. I recently found this approach to implementing check constraints in MySQL. It’s not as pretty and clean as I’d like, but, it’s the best approach that I’ve found so far.
Now. why would you want to encode business logic in the database? Can’t you make due with your ActiveRecord::Validations?
Well, have you ever updated the database directly? Have you ever called update_attribute on an object? How about save_with_validation(false)? Yeah, I thought so. Read more about why you should treat your database as a fortress in Dan Chak’s recently released book, Enterprise Rails (review coming soon).
