【问题标题】:Column type in grails not workinggrails中的列类型不起作用
【发布时间】:2015-12-11 08:22:21
【问题描述】:

这是我的模型。

class Review {
String review
Date date
int numberOfComments
String status
static belongsTo = [game:Game, user:User]
static hasMany=[comment:Comment]
static mapping ={
    numberOfComments    defaultValue: "0"
    review type: 'text'
}

static constraints = {

}

当我输入 400 个字符的文本时,它产生了这个错误

我不知道为什么评论类型:“文本”不起作用。有人可以帮忙吗?

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    事实上,grails GORM 有时在更新列类型时会出现问题,尤其是当它们包含任何数据时。尝试删除数据库中选定的列/表并重新启动应用程序。

    还要确保您在 conf/DataSource.groovy 中进行了更改

    dbCreate = "更新"

    dbCreate = "创建-删除"

    已编辑:首先我没有注意到您使用的是 h2 db。请查看this answer 了解 h2 数据库中的文本类型。

    【讨论】:

    • 我真的需要把它改成create-drop吗?我不想丢失数据库中的所有内容,这有什么区别?
    • 在开发中我使用'create-drop',在生产中我使用'update'。这是确保列/字段具有所需类型的最简单方法。
    • 所以我改为 create-drop 然后静态映射 = { review type : 'clob' }?
    • 首先更改为create-drop,不要更改映射。 h2 可能将此字段创建为常规 varchar。如果重新启动后它仍然不起作用,请将映射更改为 clob。
    • 你也在生产中使用 h2 吗?如果在移动到其他环境时使用 clob 类型,您可能会遇到问题
    【解决方案2】:

    也许你可以使用 sqlType 来代替

    class Email {
    
        String body
    
        static mapping = {
            body sqlType: "longtext"
        }
    

    【讨论】:

      【解决方案3】:

      您可以将其设为blob 类型

      static mapping = {
          review (type:’blob’)
      }
      

      注意type 是 Grails 2.0 的默认属性,如果您使用较新版本的 grails,您应该使用 sqlType

      在这里查看:http://grails.github.io/grails-doc/latest/ref/Database%20Mapping/column.html

      【讨论】:

      • 它说 Message: java.lang.String cannot be cast to java.sql.Blob
      猜你喜欢
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多