hibernate - Grails数据源 “Cannot add or update a chil

我在我的应用程序中使用了Grails数据源插件,并且在持久化(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]
}

当我尝试并坚持自己的 child 上课时,出现此错误
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)吗?如果是这样,为什么它不对实际数据源(即datasource A中)施加FK约束?

我是Grails的新手,无法在其他任何地方找到此特定问题的解决方案,如果这听起来像是一个愚蠢的问题,请对不起

最佳答案

该插件不支持跨数据库的这种关系。 GORM可能不知道另一个域类位于另一个数据库中。

解决该错误的方法是存储对父行的引用,例如:

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

看到这个:http://markmail.org/message/omllq46sn3d2l6ah

关于hibernate - Grails数据源 “Cannot add or update a child row: a foreign key constraint fails”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9952448/

相关文章:

grails - Grails UI性能插件页面压缩问题

grails - 动态Groovy上传到Grails应用程序

grails - 无法从Grails连接到MySQL

grails - 在Grails中,是否可以将数据库迁移插件配置为在其他插件之前运行?

grails - 什么是java.lang.NoClassDefFoundError:org/apa

grails - Grails Spring安全性插件镜像访问控制

json - 在Grails中编码Joda LocalTime

grails - grails从 Controller 返回xml字符串

hibernate - Grails命名查询NOT IN

grails - Grails 应用程序中的 CMS