假设名称和 id 是我的用户表上的唯一字段,并且我为这两个表设置了唯一的约束。因此,如果我有一个 id为2 ,名称为“robel” 的值,为什么下面的代码给了我验证异常?
User old = UserFindByName("robel")
old.setName("changed")
old.save(failOnError: true)
User neew = new User(name:"robel")
neew.save(failOnError: true)
我尝试设置断点,并在保存 neew 之前检查旧的值是否已更改,我确定它已更改,但最后一行给我一个异常(exception),说名称:“robel”必须唯一。这是为什么?发生了什么?
最佳答案
在添加新的一个实例之前,您应该flush changes。更改:
old.save(failOnError: true)
至:
old.save(failOnError: true, flush: true)
关于grails - 通过两次连续保存使唯一验证异常失效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35310243/