Ognjen Regoje bio photo

Ognjen Regoje
But you can call me Oggy


I make things that run on the web (mostly).
More ABOUT me and my PROJECTS.

me@ognjen.io LinkedIn

The case against database-level constraints in rails

#rails #technical

There was an article recently that mentioned using database-level constraints in Rails. It had a migration that showed how it can be done. After giving it some thought, here’re three reasons that make me think that they’re not worth it.

More difficult to implement/deploy

Firstly, they require a migration to deploy.

Second, you’re restricted to what your database supports and can’t do Ruby in them. Validating a Malaysian IC number would be interesting to say the least.

Can’t see it in the code

You’d have to make an additional effort to document them in some way. I’m a fan of the annotate models but it doesn’t support constraints so they’d have to be done manually.

More difficult to override

And most importantly they’d be a pain to override. An important part of polite software is that it be fudgable and it’d just be so much work to fudge constraints. With models you can just .save(false) but with a db level constraint I’m not entirely sure how that’d work.

So, at first look they don’t seem to be worth it.