Search code examples
mysqlgrailsgrails-orm

Grails: text instead of varchar


I need the MySQL column type for the String field in my Domain class to be TEXT or VARCHAR(3000), but nothing I try seems to work - it remains VARCHAR(255). I've tried

static mapping = {
   longString type: 'text'
} 

and

static mapping = {
   longString sqlType: 'text'
}  

and

static constraints = {
   longString (blank: true, nullable: true, maxSize: 3000)
} 

and

static constraints = {
   longString (blank: true, nullable: true, size: 0..65535)
}

MySQL Server version 5.0.95, Grails 2.4.3. I'm totally mystified and would appreciate any help..


Solution

  • You need to define the type of the column in the mapping block rather than constraints. Assuming the name of the property is longString, add this

    static mapping = {
      longString type: 'text'
    }
    

    This will create a column with a MySQL type of longtext.

    To verify that this works, try dropping your database, create a new (empty) database, restart the app and check the type of the column that is created. See this example for reference.