Search code examples
grailsgrails-orm

Setting default maxLength for Grails GORM Strings?


I know you can set default constraints via the grails.gorm.default.constraints config property by name by:

grails.gorm.default.constraints = {
    '*'(nullable:true)
}

but is there a way to set it by type? I want to default all my strings to default to maxSize:2000 (primarily to force the default db mapping to not be to varchar(255))


Solution

  • I don't think there's any way to do this easily in Config.groovy. You can create a custom dialect for hibernate without too much trouble though. For example (using the Postgres dialect):

     package mypackage;
    
     import org.hibernate.dialect.PostgreSQLDialect;
     import java.sql.Types;
    
     public MyPostgresDialect extends PostgresSQLDialect {
         public MyPostgresDialect() {
             super();
             registerColumnType(Types.VARCHAR, "text");
         }
     }
    

    Then update DataSource.groovy to use the new dialect:

    datasource {
        ...
        dialect = mypackage.MyPostgresDialect
    }