SQLite Forum

FTS5 Error database disk image is malformed after 'delete'
Login
I get this error when running the below script.

> database disk image is malformed

**The cause appears to happen when:**

1. Deleting from an external content table values that do not exists or after creation.
2. Accessing the rank column or using bm25 on said table.

**This script reproduces the error:**

```sql
CREATE TABLE test (
    id INTEGER PRIMARY KEY,
    name TEXT,
    value TEXT
);

INSERT INTO test (name, value) VALUES
    ('hello', 'world'),
    ('quick', 'the quick text'),
    ('brown fox', 'just a brown fox roaming');

CREATE VIRTUAL TABLE test_idx USING fts5(name, value, content=test, content_rowid=id);

INSERT INTO test_idx (test_idx, rowid, name, value)
    SELECT 'delete', id, name, value FROM test;

INSERT INTO test_idx (rowid, name, value)
    SELECT id, name, value FROM test;

SELECT name FROM test_idx WHERE test_idx MATCH 'quick' ORDER BY rank;
```

> Error: near line 22: database disk image is malformed