Reputation: 1370
I'm using grails 2.0 with mysql and i want some Domain attributes to be nullable. In my domain class i did:
static constraints = {
counter(nullable: true)
competitors(nullable: true)
}
After starting my grails app, it creates the correspondending sql but in my mysql table, the attributes aren't nullable, they're "NOT NULL".
CREATE TABLE `lookup_query` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`competitors` bigint(20) NOT NULL,
`counter` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Why are my mysql-attributes "NOT NULL" and not "NULL" ? I want them to be "NULL". Where is my mistake?
Upvotes: 3
Views: 1415
Reputation: 31300
The difference is in the boxed vs. unboxed types. A boxed type is a class and hence can be a null reference, while an unboxed type is a "primitive" and cannot hold a null value. GORM recognizes this and knows that you simply can't have a null value for a primitive type so it does not allow the columns in the DB to be null (even though your constraints
block might allow it) since it knows that it isn't possible in the code.
If you want a field to be nullable, your code has to allow it to be so in order for the database to be created that way.
Another option is to change the GORM default from nullable: false
to nullable: true
for all columns.
Upvotes: 1