我有一个对象 Foo,它与 Bar 具有双向一对一关系,另一个与 Baz 具有一对一关系。当我尝试使用 Foo 执行 .load 并且只给它一个 Bar 时,我收到引用完整性异常,抱怨没有 Baz。
真的应该这样吗?在现实世界的环境中,数据库中不可能没有任何匹配的 Baz 对象吗?
我尝试在固定装置负载关闭中手动设置 baz:null ,但我仍然得到同样的结果。附带说明一下,当我只设置属性(例如简单的字符串)时,一切正常。只有当我开始建立关系时。
这是使用 Grails 2.2.4、Fixtures 1.2,并且没有安装 build-test-data 插件。
编辑:我有将 Baz 指定为可为空且唯一的约束。只是为了咯咯笑,我也尝试添加blank
约束,但没有运气。
static constraints = {
baz nullable:true, unique: true, blank: true
}
编辑 2:这是代码的简化版本:
class Foo {
String someValue1
String someValue2
String whatever
Bar bar
Baz baz
static mapping = {
id composite: ['someValue1', 'someValue2'], generator: 'assigned'
columns {
bar([:]) { column name: 'some_other_value' }
baz ([insertable:false, updateable: false]) {
column name: 'some_value_1'
column name: 'some_value_2'
}
}
version: false
static constraints = {
//there are no constraints for Bar
baz nullable:true, unique:true
}
}
class Bar {
String someOtherValue
static hasMany = [foos:Foo]
static mapping = {
id generator:'assigned', name:'someOtherValue'
}
}
class Baz {
String someValue1
String someValue2
String asdf
static mapping = {
id composite: ['some_value_1', 'some_value_2']
version false
}
}
class MyTest {
def fixtureLoader
@Before
void setup() {
fixureLoader.load {
myBar(Bar, someOtherValue:"shibby")
myFoo(Foo, someValue1:"test", someValue2:"test2", bar:myBar)
//i also tried this
//myFoo(Foo, someValue1:"test", someValue2:"test2", bar:myBar, baz:null)
}
}
}
这是例外的一部分:
引起:org.h2.jdbc.JdbcBatchUpdateException:参照完整性约束违规:“FK190E74B120F4F2BC:MYSCHEMA.FOO FOREIGN KEY(SOME_VALUE_1, SOME_VALUE_2) REFERENCES MYSCHEMA.BAZ(SOME_VALUE_1, SOME_VALUE_2)”;SQL 语句:插入 MYSCHEMA.foo (whatever, some_other_value, some_value_2, some_value_1) 值 (?, ?, ?, ?, ?, ?, ?, ?) [23506-164]
编辑:对不起,我之前说错了。Bar 与 Foo 是多对一的关系。