We are using Grails 2.2.1 and are seeing an issue when we try and perform a CRUD operation on a domain class that is tied to multiple datasources.
Here is what our static mapping looks like and the results of some operations:
In this scenario the CAR table only exists in d2.
class Car {
static mapping = {
datasources(['d1','d2'])
}
}
Car.d2.listAll() //This code works and return data from d2
carObject.d2.save() //This code fails with a Table or View does not exist
Now if I add the table to d1
class Car {
static mapping = {
datasources(['d1','d2'])
}
}
Car.d2.listAll() //This code works and return data from d2
carObject.d2.save() //This code now works and inserts a row into table in d2
So it seems when you have multiple datasources the table has to exist in the first one you have listed, does anyone know a way around this?
UPDATE 10/27
I have tried injecting the actual datasource and using it to perform the CRUD operations as a workaround. Unfortunately grails does not allow you to use an injected datasource to specify where you want your changes to go.
Link: How to use injected dataSource in Grails to perform operations?
Issue seems to be worst than originally anticipated. Not only does the table have to exist in the first datasource listed, but it seems to be checking the data as well. It will not allow me to insert a record into d2 with a key that exists in d1.
UPDATE 2 10/27
After researching it seems that the validation is being run against the first dataSource listed, then the actual operation is being performed on the dataSource specified.
It now works if i pass in validate false:
carObject.d2.save(validate:false)
The only problem with this work around obviously is that I lose the validation.