CodeIgniter Forums
use CASCADING DELETE with SQLITE - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: use CASCADING DELETE with SQLITE (/showthread.php?tid=65842)



use CASCADING DELETE with SQLITE - gender - 07-31-2016

Hi,

im using CodeIgniter with a SQLite-Database.

I have many tables which have many relations to each other. They are defined with some FOREIGN KEYS and CASCADING Delete and so on.

However, I created the database with "SQLiteStudio". There when im deleting a record, the related record is deleted, too (as it suposed to be).

But when Im using CodeIgniter to delete a record, the "cascade delete"-function does not work !!!

Whats the matter?! Is there a config-option for this in code-igniter ???

Didnt find anything about this problem somewhere else (and that suprised me as well).


Hope somebody can help me.


RE: use CASCADING DELETE with SQLITE - mwhitney - 08-04-2016

https://www.sqlite.org/pragma.html#pragma_foreign_keys

Quote:As of SQLite version 3.6.19, the default setting for foreign key enforcement is OFF. [...]  To minimize future problems, applications should set the foreign key enforcement flag as required by the application and not depend on the default setting.

So, when connecting to the database, you need to execute PRAGMA foreign_keys = on; The easiest way to do this would probably be to use the simple_query() method of the database library:

PHP Code:
$this->db->simple_query('PRAGMA foreign_keys = on'); 

You should only need to do this once per connection.


RE: use CASCADING DELETE with SQLITE - gender - 08-04-2016

Hey

Thank you very much.

I already found out what you suggested. And it works.

Bye