Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails domain class, String field TEXT and LONGTEXT

Tags:

grails

In a Grails domain class, how do I set the constraint for a String field so that its MySQL column type is TEXT or LONGTEXT?

So far my best approach is to set the constraint's size:

myTextField(size:0..65535) 

which results in TEXT

myTextField(size:0..2147483646) 

results in LONGTEXT (2147483646 = 2^32 / 2 - 1 - 1)

Is there a cleaner way to specify the size? Basically I want the full range of TEXT or LONGTEXT without having to hardcode a bunch of size values.

like image 513
Steve Kuo Avatar asked Jan 11 '09 20:01

Steve Kuo


2 Answers

You can declare that in the mapping closure of your Domain class:

static mapping = {    myTextField type: 'text' } 

(See ORM DSL Documentation)

like image 194
Siegfried Puchbauer Avatar answered Oct 06 '22 11:10

Siegfried Puchbauer


using sqlType would allow you to go for finer grained constraint

Class Foo{     String myTxtAsVarchar     String myTxtAsText     String myTxtAsLtext      static mapping = {        myTxtAsVarchar  sqlType: 'varchar(255)'        myTxtAsText     sqlType: 'text'        myTxtAsLtext    sqlType: 'longText'      }     /*applying constraint on DB end + on Domain model(scaffolding/Validation)*/     static constraints = {            myTxtAsVarchar  size: 2..255            myTxtAsText     size: 2..15000      } } 

using SQL type also made available actual Blob type (byte type linked to tinyBlob by default)

more mapping detailed here: https://grails.github.io/grails-doc/latest/ref/Database%20Mapping/column.html

like image 44
Smithfield Avatar answered Oct 06 '22 11:10

Smithfield