For an ORM supporting data validation, should constraints be enforced in the database as well?
- by Ramnique Singh
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).