in Grails, Is there a way to limit the size of the column to which the enum is mapped. In the following example, i would like the column type to be char(2)
enum FooStatus {
BAR('br'), TAR('tr')
final static String id
}
class Foo {
FooStatus status
static constraints = {
status(inList:FooStatus.values()*.id,size:2..2)
}
}
both inList and size do not have any effect when exporting the schema, the column type keeps its default value (varch(255)) Maybe i could do that if i define a new UserType. Any idea ?
Thank you -ken
Since GORM 6.1 identity enum mapping can be enabled with such construct
Grails ships with an undocumented (as far as I can tell anyway) custom Hibernate mapping for enums. The class is org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType. It won't let you set the column size but does make it easy to change what is stored in the DB for each enum value without having to add transient fields to your model.
You can run an 'alter table' in Bootstrap.groovy to shrink the column:
Even easier (works at least in Grails 2.1.0+)
I don't think it's directly possible given the way enums are mapped internally in GORM. But changing the code to this works:
and
Adding the transient getter and setter allows you to set or get either the String (id) or enum value.