Search code examples
postgresqljunit5dbunit

How to insert DataSet in table with column defined as GENERATED ALWAYS using database-rider?


Trying to insert test_data.xml using DBRider @DataSet annotation.

XML contains data like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dataset SYSTEM "src\main\resources\test_dataset.dtd">
<dataset>
    <groups group_name='SG-83'/>
</dataset>

My schema looks like:

CREATE TABLE groups
(
    group_id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    group_name character varying(256) COLLATE pg_catalog."default" NOT NULL
)

Test fails with following trace:

Caused by: com.github.database.rider.core.exception.DataBaseSeedingException: Could not initialize dataset: test_data.xml
    at com.github.database.rider.core.dataset.DataSetExecutorImpl.createDataSet(DataSetExecutorImpl.java:141)
    at com.github.database.rider.core.RiderRunner.runBeforeTest(RiderRunner.java:44)
    ... 50 more
Caused by: org.dbunit.DatabaseUnitException: Exception processing table name='groups'
    at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:240)
    at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
    at com.github.database.rider.core.dataset.DataSetExecutorImpl.createDataSet(DataSetExecutorImpl.java:132)
    ... 51 more
Caused by: org.postgresql.util.PSQLException: ERROR: cannot insert into column "group_id"
  Detail: Column "group_id" is an identity column defined as GENERATED ALWAYS.
  Hint: Use OVERRIDING SYSTEM VALUE to override.

Is there any way not to override group_id column and use postgres auto generated values?


Solution

  • Found the issue by myself:

    In my "test_dataset.dtd" i have described group_id column, therefore dbunit trying to insert NULL value when there is no explicit value in "test_data.xml". So if i delete group_id definition from the .dtd file, test data goes to postgres with group_id auto generated value.