eclipse DBUnit - 违反完整性约束:外键无动作;SYS_FK_10556
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14171423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DBUnit - integrity constraint violation: foreign key no action; SYS_FK_10556
提问by maximus
I am currently coding test with dbunit(using hsqldb). However, I have a huge Problem at initializing the db:
我目前正在使用 dbunit(使用 hsqldb)进行编码测试。但是,我在初始化数据库时遇到了一个巨大的问题:
here`s the code:
这是代码:
/**
* 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);
}
}
my create statement looks like that:
我的 create 语句如下所示:
CREATE TABLE Rating
(
rid INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
mid INTEGER FOREIGN KEY REFERENCES Movie(movieId),
rating INTEGER NOT NULL,
);
and my startDatabaseRating.xml looks like that:
我的 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>
When I run the tests I get:
当我运行测试时,我得到:
java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: foreign key no action; SYS_FK_10556 table: RATING
java.sql.SQLIntegrityConstraintViolationException:违反完整性约束:外键无动作;SYS_FK_10556 表:评级
Why do I get this exception, cause there is still a dataset in the *.xml file. How to fix this problem?
为什么会出现此异常,因为 *.xml 文件中仍有数据集。如何解决这个问题?
UPDATE:
更新:
CREATE TABLE Movie
(
movieId INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
title VARCHAR(255) NOT NULL,
moviePath VARCHAR(500) NOT NULL
);
回答by a_horse_with_no_name
When inserting a rating, the movie that is referenced must already exist in the movie
table. So you are inserting the rows in the wrong order.
插入评级时,引用的电影必须已存在于movie
表中。所以你以错误的顺序插入行。
You need to first insert the movies, thenthe ratings.
您需要先插入电影,然后是收视率。
<?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>
回答by Rimey
Also I would recommand you to use regular labels for your columns, especially for the id and the foreign key, because it is much more easier to understand and to maintain, if another person is in charge of your code some day !
此外,我建议您为您的列使用常规标签,尤其是 id 和外键,因为如果有一天另一个人负责您的代码,它会更容易理解和维护!
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<Movie id="0" title="Movie1" moviePath="C" />
<Movie id="1" title="Movie2" moviePath="D" />
<Rating id="0" movie_id="0" rating="1" />
<Rating id="1" movie_id="0" rating="2" />
<Rating id="2" movie_id="0" rating="3" />
<Rating id="3" movie_id="0" rating="4" />
</dataset>