gpt4 book ai didi

grails - Grails 中的 Fixtures 插件引用完整性异常

转载 作者:行者123 更新时间:2023-12-02 14:44:27 24 4
gpt4 key购买 nike

我有一个对象 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
}

编辑 2 :这是代码的简化版本:
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)
}
}
}

这是异常(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]



编辑 : 对不起,我之前说错了。 Bar 与 Foo 是多对一的关系。

最佳答案

您的代码简化版本(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 的限制是可选的。
  • fixture 按预期工作。
  • 列按预期在 Foo 中创建。
  • logSql显示预期的 DML。
  • 为了见证表格的变化,我还在开发模式下引导了相同的测试数据,遵循 run-app。 .我能够使用 dbconsole 查看包含数据的预期表结构.
  • 遵循一对一双向的一般方式(称为注释代码),在子表中创建 FK [ BarBaz ],因此您在示例代码中提供的显式映射不适用。
  • 如果关系的拥有方和拥有 hasMany 背后的理由,问题会更清楚。在酒吧中提到。
  • 关于grails - Grails 中的 Fixtures 插件引用完整性异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18133326/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com