gpt4 book ai didi

database - DBUnit 坚持为未指定的值插入 null,但我希望使用 DB 默认值

转载 作者:行者123 更新时间:2023-11-29 13:44:18 26 4
gpt4 key购买 nike

我遇到了导致 SQL 插入错误的 DBUnit 问题。假设我的 dbunit testdata.xml 文件中有这个:

<myschema.mytable id="1" value1="blah" value2="foo" />

我有一个这样的表(postgres)

myschema.mytable 有一个 id、value1、value2 和一个日期字段,例如“lastmodified”。 lastmodified 列是带有修饰符“not null default now()”的时间戳

似乎 dbunit 读取表元数据并尝试为未在我的 testdata.xml 文件中指定的任何列插入空值。所以上面的 xml 结果是这样的插入:

insert into myschema.mytable (id,value1,value2,lastmodified) values (1,'blah','foo',null) 

运行测试时(dbunit/maven 插件)我得到这样的错误:

Error executing database operation: REFRESH: org.postgresql.util.PSQLException: ERROR: null value in column "lastmodified" violates not-null constraint

有什么方法可以告诉 DBUnit 不要在我未指定的字段上插入空值吗?

编辑:使用 dbunit 2.5.3、junit 4.12、postgressql 驱动程序 9.4.1208

最佳答案

使用 dbUnit“排除列”功能: How to exclude some table columns at runtime?

The FilteredTableMetaData class introduced in DbUnit 2.1 can be used in combination with the IColumnFilter interface to decide the inclusion or exclusion of table columns at runtime.

FilteredTableMetaData metaData = new FilteredTableMetaData(originalTable.getTableMetaData(), new MyColumnFilter());
ITable filteredTable = new CompositeTable(metaData, originalTable);

关于database - DBUnit 坚持为未指定的值插入 null,但我希望使用 DB 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50878231/

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