- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个对象 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
}
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)
}
}
}
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]
最佳答案
您的代码简化版本(Bar 中的 hasMany
除外)适用于我,没有任何 FK 异常。尽管如果我对父子映射正确无误,我更希望采用不同的方法来实现真正的一对一双向关系。
下面是我的设置,它在没有 FK 约束异常的情况下工作正常。请注意,我还在评论中提到我将如何实现真正的一对一双向假设 Foo 有一个 Bar 和一个 Baz .
class Foo implements Serializable{
String someValue1
String someValue2
String whatever
//True one to one can be achieved by doing as below
//static hasOne = [bar: Bar, baz: Baz]
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 = {
//baz nullable:true, unique:true
}
}
class Bar {
String someOtherValue
//True one to one can be achieved by doing as below
//Below entry makes the relation bi-directional
//Foo foo
static mapping = {
id generator:'assigned', name:'someOtherValue'
//Optional, added for clarity
someOtherValue column: 'some_other_value'
}
}
class Baz implements Serializable{
String someValue1
String someValue2
String asdf
//True one to one can be achieved by doing as below
//Below entry makes the relation bi-directional
//Foo foo
static mapping = {
id composite: ['someValue1', 'someValue2']
//Optional, added for clarity
someValue1 column: 'some_value_1'
someValue2 column: 'some_value_2'
asdf column: 'asdf'
version false
}
}
class MyTests extends GroovyTestCase {
def fixtureLoader
void setUp() {
fixtureLoader.load {
myBar(Bar, someOtherValue:"shibby")
myFoo(Foo, someValue1:"test", someValue2:"test2",
whatever: "whatever", bar: myBar)
}
}
void testSomething() {
Foo.all.each{println it.properties}
Bar.all.each{println it.properties}
}
}
//Bootstrap Test in Dev mode
new Bar(someOtherValue: 'shibby').save()
new Foo(someValue1:"test", someValue2:"test2",
whatever: "whatever", bar: myBar).save(failOnError: true, flush: true)
hasMany
酒吧的关系。 Baz
的限制是可选的。 logSql
显示预期的 DML。 run-app
。 .我能够使用 dbconsole
查看包含数据的预期表结构. Bar
和 Baz
],因此您在示例代码中提供的显式映射不适用。 hasMany
背后的理由,问题会更清楚。在酒吧中提到。 关于grails - Grails 中的 Fixtures 插件引用完整性异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18133326/
我想要一些概念上的澄清。为了证明问题是 NP 完全的,我们使用归约。 现在假设我有 L<=L'。是从 L 减少到 L' 还是我也可以用相反的方式来减少?即我能否证明如果 L 可以使用 L' 求解,那么
考虑不相交的哈密顿路径问题: 输入:一个可能是有向或无向的图 输出:此图是否至少存在 2 条边不相交的哈密顿路径?边不相交意味着没有一条边被两条路径共享。 证明不相交哈密顿路径是 np-完全的。 有人
我理解为什么有界度生成树被认为是度数为 2 的 NP 完全(这是哈密顿路径问题的一个实例),但我不明白为什么这适用于度数 > 2。如果有人可以解释一下为什么这是大于 2 的 NP 完全问题,这将是最有
我正在实现一个 Django 网站,其中上传的文件在保存到服务器 (/media) 之前使用用户提供的 key 进行加密。当用户希望查看它们时,系统会提示他们输入 key ,加密文件被解密,然后显示给
我想用nodejs列出指定目录中的所有文件。 var fs = require('fs'); var path = require('path'); var walk = function(direc
在我的文件夹 assets/data 中,有很多包含我的应用静态数据的 XML 文件。 对于某人来说,检索 APK、修改其中的一部分并安装到设备上真的很容易。 我想通过检查我的 assets/data
我正在努力将我的备份脚本从 shell 转换为 Python。我的旧脚本的功能之一是通过执行以下操作检查创建的 tarfile 的完整性:gzip -t。 这在 Python 中似乎有点棘手。 似乎唯
我正在尝试将包含带有单独 CSS 和 js 文件的 HTML 脚本的 php 文件导入另一个包含我的页眉和页脚的 php 文件。页眉和页脚来自一个模板,该模板使用非常困惑和令人费解的 CSS,基本上对
使用 Flask,我试图验证 cookie 没有被篡改。现在,如果我更改 cookie 值,它只会抛出一个错误,但我想检查代码 is_valid(session['user_id']) 并重定向/重置
在 PHP(和 MySQL)中,我们有许多技术来确保输入的数据有效且安全。添加斜杠、MySQL 的转义字符串和正则表达式是我们经常使用的一些。 我已经看到此链接,该链接对该主题进行了非常初步的介绍,但
下面的代码使用了不安全的 GeneralizedNewtypeDeriving扩展中断 Data.Set通过插入具有不同 Ord 的不同元素实例: {-# LANGUAGE GeneralizedNe
我刚刚在 NPM 上创建了一个新包(这非常简单),我对如何维护包的完整性感兴趣。任何人都可以发布软件包的新版本吗?或者这仅限于我的用户帐户? 如果任何人都可以发布对包的更改,如何监控他们的修改以确保项
我正在尝试使用 Dapper 和 SQLite 来追踪 C# 项目中的数据库损坏错误。所以我正在寻找一种方法来检查代码中的数据库完整性。我发现多个地方说我可以为此发送命令“PRAGMAintegrit
yarn 安装抛出: EACCES: permission denied, unlink '/home/minnak/Darbas/market/node_modules/.yarn-integrit
上下文: 我有 open-sourced a repository ,由 Travis-CI 测试。特拉维斯提供 build-notification用于测试运行的钩子(Hook),因此您可以在 IR
我是一名优秀的程序员,十分优秀!