【问题标题】:DBUnit insists on inserting null for unspecified values, but I want the DB default value to be usedDBUnit 坚持为未指定的值插入 null,但我希望使用 DB 默认值
【发布时间】:2018-11-25 10:56:26
【问题描述】:

我遇到了这个 DBUnit 问题,导致 SQL 插入错误。假设我的 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

【问题讨论】:

    标签: database postgresql junit4 dbunit spring-test-dbunit


    【解决方案1】:

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

    DbUnit 2.1 中引入的FilteredTableMetaData 类可以与IColumnFilter 接口结合使用,在运行时决定表列的包含或排除。

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

    【讨论】:

      猜你喜欢
      • 2011-03-24
      • 2011-07-20
      • 2015-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 2014-11-19
      相关资源
      最近更新 更多