Reputation: 33307
In my Grails domain class I want to set default values which do persist in the database. I use mysql as database. I tried to do this:
class A {
long someValue = 1
long someOtherValue
boolean someBool = true
boolean someOtherBool
static mapping = {
someOtherValue defaultValue: 1
someOtherBool defaultValue: true
}
}
But nothing works. There are no default values set in the database. What do I have to change to get my default values being set correctly?
Upvotes: 6
Views: 14864
Reputation: 23
class A {
long someValue
long someOtherValue
boolean someBool = Boolean.TRUE
boolean someOtherBool = Boolean.TRUE
static mapping = {
someValue defaultValue: '1'
someOtherValue defaultValue: '1'
}
}
This will work, tested in 2.2.3.
Upvotes: 2
Reputation: 716
I found that for defaultValue to work with String properties, I needed to put double quotes around single quotes and for defaultValue to work for numeric properties, I needed to put double quotes around the number or the defaults wouldn't appear in the DDL. So, for instance:
static mapping = {
myStringProperty defaultValue: "'Cash'"
myIntProperty defaultValue: "0"
}
Also, as far as I can tell, default values do not work for properties that are enums.
Upvotes: 2
Reputation: 5538
If you are on Grails 2.2 above then you can use defaultValue. Look at Burt's answer here Try it, hope this helps:
Class A {
Long someValue
Long someOtherValue
Boolean someBool
Boolean someOtherBool
static mapping = {
someOtherValue defaultValue: 1
someOtherBool defaultValue: true
...
}
}
Upvotes: 6