For an ORM supporting data validation, should constraints be enforced in the database as well?

Posted by Ramnique Singh on Programmers See other posts from Programmers or by Ramnique Singh
Published on 2012-06-21T05:30:11Z Indexed on 2012/06/21 9:23 UTC
Read the original article Hit count: 213

Filed under:
|
|

I have always applied constraints at the database level in addition to my (ActiveRecord) models. But I've been wondering if this is really required?

A little background

I recently had to unit test a basic automated timestamp generation method for a model. Normally, the test would create an instance of the model and save it without validation. But there are other required fields that aren't nullable at the in the table definition, meaning I cant save the instance even if I skip the ActiveRecord validation. So I'm thinking if I should remove such constraints from the db itself, and let the ORM handle them?

Possible advantages if I skip constraints in db, imo -

  • Can modify a validation rule in the model, without having to migrate the database.
  • Can skip validation in testing.

Possible disadvantage?

If its possible that ORM validation fails or is bypassed, howsoever, the database does not check for constraints.

What do you think?

EDIT In this case, I'm using the Yii Framework, which generates the model from the database, hence database rules are generated also (though I could always write them post-generation myself too).

© Programmers or respective owner

Related posts about database

Related posts about orm