Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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))

like image 398
Dan G Avatar asked Oct 17 '13 16:10

Dan G


1 Answers

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
}
like image 98
ataylor Avatar answered Sep 28 '22 02:09

ataylor