【问题标题】:Can't get the GORM mongodb plugin to save a new record, even with an explicit flush即使使用显式刷新,也无法让 GORM mongodb 插件保存新记录
【发布时间】:2012-03-08 22:42:40
【问题描述】:

我所知道的:mongod 正在运行,数据库存在,集合存在,我可以使用 mongodb 控制台进行插入,并且通过 Grails 建立到 mongod 实例的连接(即我可以查询等等)。

其他细节:Grails 2.01、mongodb plugin 1.0.0 RC4、hibernate plugin 2.01、mongodb 2.0.3

class Node {
    static mapWith = "mongo"

    static constraints = {
    }

    ObjectId id
    Integer someId //someId can be null or just non-existent
    String name
}

什么不起作用:我无法通过 GORM 插入任何数据,即使我明确地将 flush 设置为 true。

def n = com.company.project.Node(name: "test")
n.save(flush:true)

我也在 Grails 控制台中尝试过,但没有错误消息。是不是我做错了什么?

【问题讨论】:

  • 使用 save(failOnError:true) 查看错误信息。我的猜测是 someId 在约束中需要为空。
  • 默认情况下,GORM 不允许将空值插入数据库,但您可以通过在 Config.groovy 中将其关闭全局: grails.gorm.default.constraints = { '*'(nullable: true ) }

标签: grails mongodb grails-orm grails-plugin grails-2.0


【解决方案1】:

我是 Grails 的新手,所以其他人可能会也可能不会纠正我。无论如何,问题是 Mongodb GORM 插件默认情况下不允许您在 Mongodb 域类中指定的任何字段的空值。

到目前为止,我不确定如何覆盖此行为,但如果我弄明白了,我会更新它。

【讨论】:

    【解决方案2】:

    您是否尝试过让您的属性可以为空?像这样的:

    static constraints = 
    {
       someId nullable: true
    }
    

    【讨论】:

    • 抱歉回复晚了 - 是的,我相信我做到了。我不确定这是否重要,但我正在将 GORM 与 MySQL 一起使用。
    【解决方案3】:

    保存前执行validate()方法看看结果,很可能有一些你可能不知道的约束错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-09
      • 2020-06-16
      • 2016-07-02
      • 2011-09-11
      • 2021-10-31
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      相关资源
      最近更新 更多