Peewee: Cascade delete is not working as expected

Created on 5 Jan 2017  路  3Comments  路  Source: coleifer/peewee

Hi Charles,

i created a table which is related to another table by a ForeignKeyField and on='CASCADE' policy in Sqlite.
It seems like if i delete a record from the parent table, the children are not deleted.
Here is a test that proves that behavior:
https://gist.github.com/aellwein/9e814769196e5524e94199163aa4a0bd

Am i doing something wrong?
Thanks!
Alex

All 3 comments

Looks like sqlite by default has foreign key support turned off:

http://stackoverflow.com/a/13641365/724251

Need to add something like:

Tweet.raw("PRAGMA foreign_keys=ON").execute()

Might be a nicer way included somewhere in the peewee docs.

@alexlatchford
thanks for pointing it out.
Seems to be mentioned in the documentation, in http://docs.peewee-orm.com/en/latest/peewee/database.html?#additional-connection-initialization

Although imho it is a little bit scary, that referential integrity is turned off by default.

@aellwein -- don't use SQLite if you don't know how it works. Simple as that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

megachweng picture megachweng  路  3Comments

lucasrc picture lucasrc  路  5Comments

ghost picture ghost  路  5Comments

mikemill picture mikemill  路  3Comments

ezk84 picture ezk84  路  4Comments