gpt4 book ai didi

java - DBUnit - 违反完整性约束 : foreign key no action; SYS_FK_10556

转载 作者:搜寻专家 更新时间:2023-11-01 01:17:38 25 4
gpt4 key购买 nike

我目前正在使用 dbunit(使用 hsqldb)进行编码测试。但是,我在初始化数据库时遇到了一个大问题:

这是代码:

/**
* Init before a test starts
*/
@Before
public void initialise() {
IDataSet dataSetRating = null;
IDataSet dataSetMovie = null;

log.info("enter init test");

try {
con = datasource.getConnection();
icon = new DatabaseConnection(con);
File rating = new File("./src/test/resources/startDatabaseRating.xml");
dataSetRating = new FlatXmlDataSetBuilder().build(rating);
DatabaseOperation.CLEAN_INSERT.execute(icon, dataSetRating);
} catch (Exception e) {
e.printStackTrace();
log.error(e);
System.exit(-1);
}
}

我的创建语句如下所示:

CREATE TABLE Rating
(
rid INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
mid INTEGER FOREIGN KEY REFERENCES Movie(movieId),
rating INTEGER NOT NULL,
);

我的 startDatabaseRating.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<Rating rid="0" mid="0" rating="1" />
<Rating rid="1" mid="0" rating="2" />
<Rating rid="2" mid="0" rating="3" />
<Rating rid="3" mid="0" rating="4" />

<Movie movieid="0" title="Movie1" moviePath="C" />
<Movie movieid="1" title="Movie2" moviePath="D" />
</dataset>

当我运行测试时,我得到:

java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: foreign key no action; SYS_FK_10556 table: RATING

为什么我会得到这个异常,因为*.xml 文件中仍然有一个数据集。如何解决这个问题?

更新:

CREATE TABLE Movie
(
movieId INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
title VARCHAR(255) NOT NULL,
moviePath VARCHAR(500) NOT NULL
);

最佳答案

插入评级时,引用的电影必须已经存在于 movie 表中。所以你以错误的顺序插入行。

您需要先插入电影,然后评级。

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<Movie movieid="0" title="Movie1" moviePath="C" />
<Movie movieid="1" title="Movie2" moviePath="D" />

<Rating rid="0" mid="0" rating="1" />
<Rating rid="1" mid="0" rating="2" />
<Rating rid="2" mid="0" rating="3" />
<Rating rid="3" mid="0" rating="4" />
</dataset>

关于java - DBUnit - 违反完整性约束 : foreign key no action; SYS_FK_10556,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14171423/

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