Reputation: 3794
Many times i find myself in a dilemma when designing a new domain class. When i create a new domain class in Grails i have few properties. I can either add not null constraints on these properties or make them nullable. This choice seems optional in many cases in the domains i create. So, what are some of the good reasons to add not nullable constraints and also not blank constraint. I need few points to understand when should one care about these constraints. Thanks!
static constraints = {
title nullable:true, blank:true
}
Upvotes: 0
Views: 354
Reputation: 2678
Scenarios where,
we add constraint
:- nullable :false, blank:false
identity
value(importance)
reducing query complexity
null
or blank
just because of business logic.Upvotes: 1
Reputation: 37073
nullable
means, that the attribute there may be devoid of value. This is also how (SQL) databases model that - so this setting will end up in your table constraints with your (SQL) database (e.g. NOT NULL
).
Also (SQL) databases can query for that: is null
and is not null
. This might lead to some surprising results (e.g. with order).
Being nullable
makes e.g. an Boolean tri-state. It can be true
, false
, or null
- so /yes/, /no/, or /no decision made/ (or revoked).
When modelling your DB with GORM a common place for nullable
is for 0..1 associations.
Or in short: think of Optional
or Maybe
.
Upvotes: 1
Reputation: 1501
You should be thinking about your models... Take a Widget. What attributes are necessary to constitute a Widget? Perhaps it's just a name, or a part number, and all other attributes are nice-to-have's or extra details. Maybe its a name AND part number. Necessary attributes would receive the nullable:false. Any others could be nullable:true.
blank, true or false? Well, I don't see much utility in a blank string as a value. I'd rather deal with a simpler case or null versus not null to indicate if there is data present.
Upvotes: 1