[英]Grails domain class, String field TEXT and LONGTEXT
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? 在Grails域类中,如何为String字段设置约束,使其MySQL列类型为TEXT或LONGTEXT?
So far my best approach is to set the constraint's size: 到目前为止,我最好的方法是设置约束的大小:
myTextField(size:0..65535)
which results in TEXT 这导致TEXT
myTextField(size:0..2147483646)
results in LONGTEXT (2147483646 = 2^32 / 2 - 1 - 1) 结果在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. 基本上我想要全系列的TEXT或LONGTEXT,而不必硬编码一堆大小值。
You can declare that in the mapping closure of your Domain class: 您可以在Domain类的映射闭包中声明:
static mapping = {
myTextField type: 'text'
}
(See ORM DSL Documentation ) (参见ORM DSL文档 )
using sqlType would allow you to go for finer grained constraint 使用sqlType将允许您获得更细粒度的约束
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) 使用SQL类型也可以使用实际的Blob类型(默认情况下链接到tinyBlob的字节类型)
more mapping detailed here: https://grails.github.io/grails-doc/latest/ref/Database%20Mapping/column.html 更多映射详述: https : //grails.github.io/grails-doc/latest/ref/Database%20Mapping/column.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.