ZF-8292: Zend_Test_PHPUnit_Db_Operation_Truncate fails on Postgres tables with foreign keys


If a Postgres database has tables with foreign keys, Zend_Test_PHPUnit_Db_Operation_Truncate fails with:

[SQLSTATE[0A000]: Feature not supported: 7 ERROR: cannot truncate a table referenced in a foreign key constraint DETAIL: Table "foo" references "bar". HINT: Truncate table "foo" at the same time, or use TRUNCATE ... CASCADE.]

The attached trivial patch fixes the problem on Postgres 8.3, but I don't know if it will work for older versions of Postgres. From what I can tell, the CASCADE parameter was added in 8.2. In 8.1, the documentation just says to TRUNCATE all the tables at once.



Trivial patch that fixes the issue for Postgres 8.3

Remove patch from the bug description

Assigning to Ben.

Can you post an example of your XML fixture plus a rough description how your foreign keys are set?

The Truncate Operation uses a reverse iterator, so it should truncate the tables in the correct order, when you specify them in a correct order.

Maybe this is an documentation issue in regards to foreign keys.

I saw the previously-fixed bug about truncating tables in reverse order, but my read of the Postgres documentation was that it just flat-out rejects the bare truncate command, even if it wouldn't cause any cascading deletes. "TRUNCATE cannot be used on a table that has foreign-key references from other tables, unless all such tables are also truncated in the same command. Checking validity in such cases would require table scans, and the whole point is not to do one. The CASCADE option can be used to automatically include all dependent table..."

In other words, unless you have CASCADE in there, it will just look at the table metadata, see that there are foreign-key references, and stop.

If you still think it's because of my specific setup, I can attach a sample XML fixture.

Hm ok that i didnt know about postgres truncate. Will fix it.

Fixed and merged into 1.9 release branch