Hibernate “Grails数据源”;无法添加或更新子行:外键约束失败;

Hibernate “Grails数据源”;无法添加或更新子行:外键约束失败;,hibernate,grails,datasource,Hibernate,Grails,Datasource,我在应用程序中使用Grails Datasources插件,在持久化引用只读关联(父)类的(chidl)域类时遇到问题。例如: /* Parent domain class; a read-only datasource using the Datasources plugin */ class Parent { //...Some fields } /* Child domain class, referencing the parent class */ class Child /

我在应用程序中使用Grails Datasources插件,在持久化引用只读关联(父)类的(chidl)域类时遇到问题。例如:

/* Parent domain class; a read-only datasource using the Datasources plugin */
class Parent {
  //...Some fields
}

/* Child domain class, referencing the parent class */
class Child
  // Some fields
  static hasOne = [parent:Parent]
}
当我尝试持久化我的子类时,我得到了这个错误

Cannot add or update a child row: a foreign key constraint fails 
(`foo`.`child`, CONSTRAINT `FK38A5EE5F707D1A2B` 
FOREIGN KEY (`id`) REFERENCES `parent` (`id`))
我注意到,在启动时创建表时,Grails会创建一个引用表的本地空副本,从(只读)
datasource a
到我的本地(可写)
datasource B
。它是否会引用此表(及其新创建的FK)?如果是这样,为什么不对实际数据源(即在
数据源A
中)实施FK约束


我是Grails的新手,在其他任何地方都找不到这个特定问题的解决方案,如果这听起来像个愚蠢的问题,那么很抱歉,插件不支持跨数据库的这种关系。GORM可能不知道另一个域类位于另一个数据库中

解决此错误的方法是存储对父行的引用,类似于:

class Child{
      long parentId      
      Parent getParent(){
           Parent.get( parentId )
      }
}

查看此链接:

感谢您的建议和链接。你能告诉我我的二传手长什么样吗?我不也需要家长推荐吗?此外,我注意到,尽管将该字段标记为瞬态,但在我删除并重新创建数据库/表时,它仍然在只读数据源中创建了对父表的外键引用-为什么?您的setter将类似于void setParent(Parent){parentId=Parent.id}它可能会创建父级,因为dbCreate是在数据源中设置的,而GORM不是那么聪明。再次感谢Tomas。不幸的是,我仍然得到FK错误。这是我的子类:
class child{
static transients=['parentId']
Parent getParent(){Parent.get(parentId)}
void setParent(p){this.parentId=p.id}
我在只读数据源上设置了dbCreate('update')属性您的transient应该是Parent。