I have an object Foo that has a bidirectional one-to-one relationship with Bar and another one with Baz. When I try to do a .load with Foo and only give it a Bar, I get referential integrity exceptions complaining that there isn't a Baz.
Should this really be the case? In a real world environment isn't it possible that there wouldn't be any matching Baz object in the database?
I tried manually setting baz:null in the fixtures load closure, but I still get the same thing. On a side note, when I only set properties (such as a simple string), everything works fine. It's only when I start setting relationships.
This is with Grails 2.2.4, Fixtures 1.2, and without the build-test-data plugin installed.
EDIT: I have the constraints specifying Baz to be nullable and unique. Just for giggles I tried adding the blank
constraint too, but no luck.
static constraints = {
baz nullable:true, unique: true, blank: true
}
EDIT 2: Here is a simplified version of the code:
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)
}
}
}
Here is part of the exception:
Caused by: org.h2.jdbc.JdbcBatchUpdateException: Referential integrity constraint violation: "FK190E74B120F4F2BC: MYSCHEMA.FOO FOREIGN KEY(SOME_VALUE_1, SOME_VALUE_2) REFERENCES MYSCHEMA.BAZ(SOME_VALUE_1, SOME_VALUE_2)"; SQL statement: insert into MYSCHEMA.foo (whatever, some_other_value, some_value_2, some_value_1) values (?, ?, ?, ?, ?, ?, ?, ?) [23506-164]
EDIT: Sorry, I misspoke earlier. Bar has a many-to-one relationship with Foo.