CodeIgniter Forums
CI 1.6.1 Postgre driver, cannot truncate a table referenced in a foreign key constraint - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: CI 1.6.1 Postgre driver, cannot truncate a table referenced in a foreign key constraint (/showthread.php?tid=8127)



CI 1.6.1 Postgre driver, cannot truncate a table referenced in a foreign key constraint - El Forum - 05-06-2008

[eluser]piotrpolak[/eluser]
It is not a reall bug.

I have got 2 tables:

Code:
CREATE TABLE table1 (
  myid serial NOT NULL PRIMARY KEY
);

Code:
CREATE TABLE table2 (
  myid int NOT NULL REFERENCES table1(myid) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
);

Lets suppose table2 is empty, and we try to truncate table1. This is what we get:

Code:
ERROR: cannot truncate a table referenced in a foreign key constraint

Method truncate from CI_DB_postgre_driver:
Code:
function _truncate($table)
{
        return "TRUNCATE ".$this->_escape_table($table);
}

Should/could be:

Code:
function _truncate($table)
{
        return "TRUNCATE ".$this->_escape_table($table)." CASCADE"; // HERE!
}