Skip to content

Triggers that insert into fts5 tables fail to transact #654

Open
@aeroheim

Description

@aeroheim

I've got a bit of a strange error that I'm seeing under oddly specific circumstances.

In essence, when a trigger that inserts into an fts5 table runs after a statement using a RETURNING clause, the trigger itself (I presume) fails to transact and results in the following error: SqliteError: cannot commit - no transaction is active.

The example code below should reproduce the issue:

const Database = require('better-sqlite3');
const db = new Database(':memory:');

db.exec(`
  CREATE TABLE a (id INTEGER PRIMARY KEY, name TEXT);
  CREATE VIRTUAL TABLE b USING fts5(name);
  CREATE TRIGGER a_trigger AFTER INSERT ON a
  BEGIN
    INSERT INTO b (name) VALUES ('test');
  END;
`);

db.transaction(() => {
  // SqliteError: cannot commit - no transaction is active
  const { id } = db.prepare(`INSERT INTO a VALUES (0, 'test') RETURNING id;`).get();
})();

Running the same statements in the sqlite repl succeeds without any issues:

λ sqlite3
SQLite version 3.35.5 2021-04-19 18:32:05
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> CREATE TABLE a (id INTEGER PRIMARY KEY, name TEXT);
sqlite> CREATE VIRTUAL TABLE b USING fts5(name);
sqlite> CREATE TRIGGER a_trigger AFTER INSERT ON a BEGIN INSERT INTO b (name) VALUES ('test'); END;
sqlite> BEGIN TRANSACTION; INSERT INTO a VALUES(0, 'test') RETURNING id; COMMIT;
0

And here's a simple sandbox that contains the code above and reproduces the issue. You can run yarn start or node index/src.js on the sandbox's terminal to run the code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions