SQLite Forum

A testcase causing Assertion `memIsValid(&aMem[pOp->p1])' failed
Login
Thanks for the bug report.

This is a real, long-standing bug. The problem goes all the way back to 2009,
when foreign key constraints were first introduced into SQLite version 3.6.19.
A simplified test case is as follows:

> ~~~
CREATE TABLE t1(a PRIMARY KEY,b);
CREATE TABLE t2(c PRIMARY KEY REFERENCES t1 ON DELETE CASCADE,d);
PRAGMA foreign_keys = ON;
DROP TABLE t1;
CREATE TABLE t1(x PRIMARY KEY, y);
INSERT INTO t1 VALUES(1,2);
DELETE FROM t1;
~~~

Here is what was happening: The "ON DELETE CASCADE" in the definition
of table t2 generates a virtual trigger that performs the cascading
delete.  This virtual trigger is cached in the schema definition for the
t2 table.  But when the definition of table t1 changes (due to the
DROP and subsequent CREATE TABLE) the cache of the virtual cascade-trigger
was not being cleared.  With the new definition of table t1, the older,
cached trigger implementation is no longer valid.

[The fix](src:/info/5232c9777fe4fb13), which is now on trunk, is to reset the cache of triggers used for
cascading deletes and updates after every schema change.