sebastianbergmann/dbunit — Issue #58: Truncate on PostgresSQL does not reinitialize sequences values
Author: MaxEvron
Created: 2012-03-09 16:17:26+00:00
State: open

Running DBUnit with fixtures may cause exceptions to be fired when trying to save new objects to database (for example with an ORM engine like Doctrine) or make asserts fail (especially `assertTablesEqual`) because of duplicate primary keys or primary keys not the same at each test run-tine. Example with the following:

stud_id
stud_name
stud_surname

1
John
DOE

2
Jane
DOE

3
Jack
DOE

Running the code above may fail with a unique key constraint exception fired by Postgres as Doctrine will try to store a new `Student` object, using sequence next value whereas a `Student` already exists in database for the computed `stud_id` number.

$objStudent = new Student();
$objStudent->setSurname('James');
$objStudent->setName('DOE');
$objEntityManader->persist($objStudent);
$objEntityManader->flush(); // Exception may occur here

Same thing with the following:

$objTableQuery = $this->getConnection()->createQueryTable('student', 'SELECT * FROM student ORDER BY stud_id');
$objTableExpected = $this->createXmlDataSet(__DIR__ . '/PersistStudent.xml')->getTable('student');
$this->assertTablesEqual($objTableExpected, $objTableQuery);

having PersistStudent.xml as follow :

stud_id
stud_name
stud_surname

1
John
DOE

2
Jane
DOE

3
Jack
DOE

4
James
DOE

Here, we cannot figure out if `stud_id` will be 4 or something else, making `assertTablesEqual` fail. In fact, it may fail most of the time.

I'll try to find a fix and post it here.