Welcome Guest, Not a member yet? Register   Sign In
use CASCADING DELETE with SQLITE
#1

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.
Reply
#2

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.
Reply
#3

Hey

Thank you very much.

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

Bye
Reply




Theme © iAndrew 2016 - Forum software by © MyBB