Search code examples
grailsgrails-orm

How to define an integer attribute length on Grails (GORM)?


I like to create a domain class with an integer attribute who should be mapped at database with a length of 2, by default GORM creates an INT(11).

Ex:

class MyDomain {
    int options
    static constraints = {
        options(min: 0, max: 99, maxSize: 2)
        // options(size: 2) -> Size are not available for integer attribute.
    }
    static mapping = {
        options(length: 2) // Does't work too
    }
}

Obs.: I'm using Grails v2.5.2.


Solution

  • For Integer attributes you can use sqlType in your mapping :

    static mapping = {
       options sqlType: 'INT(2)'
    }
    

    More info you about mapping you can find here : http://grails.github.io/grails-doc/2.5.1/ref/Database%20Mapping/column.html