#Create trigger makes `sql.simple()` fail silently

1 messages · Page 1 of 1 (latest)

burnt loom
#

I'm trying to run a sql script (sqlite) that creates a database schema but it is silently failing. In this script there are two CREATE TRIGGER, that, when removed, makes the script run as intended. Can someone tell me if it is a bug or working as intended? Code examples

Failing example:

const sql = new Bun.SQL("sqlite://:memory:?_foreign_keys=on&_journal_mode=WAL");
await sql`
CREATE TABLE "DUMMY"(
    "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "USERNAME" TEXT NOT NULL
);

CREATE TABLE "USER"(
    "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "USERNAME" TEXT NOT NULL UNIQUE,
    "PASSWORD" TEXT NOT NULL,
    "CREATED_AT" INTEGER NOT NULL DEFAULT (unixepoch())
);

CREATE TRIGGER "TEST_TRIGGER"
BEFORE INSERT ON "USER"
BEGIN
    SELECT UNIXEPOCH();
END;
`.simple();

// this fails. When connecting to a file and not :memory:
// I see that only the first command is executed, no matter
// how many commands come after or 
const users = await sql`SELECT * FROM "USER"`;

console.log(users);

Working example:

const sql = new Bun.SQL("sqlite://:memory:?_foreign_keys=on&_journal_mode=WAL");
await sql`
CREATE TABLE "DUMMY"(
    "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "USERNAME" TEXT NOT NULL
);

CREATE TABLE "USER"(
    "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "USERNAME" TEXT NOT NULL UNIQUE,
    "PASSWORD" TEXT NOT NULL,
    "CREATED_AT" INTEGER NOT NULL DEFAULT (unixepoch())
);
`.simple();

const users = await sql`SELECT * FROM "USER"`;

// prints '[]'
console.log(users);

The original code was using sql.file('script.sql'), tried changing to sql.file('script.sql').simple() and reading the file contents and running as the examples.

#

Running the script directly in sqlite3 works.

$ sqlite3 test.db
SQLite version 3.51.0 2025-11-04 19:38:17
Enter ".help" for usage hints.
sqlite> .read migrations/000001-creation.up.sql
sqlite> select * from sqlite_master;
table|DUMMY|DUMMY|2|CREATE TABLE "DUMMY"(
    "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "USERNAME" TEXT NOT NULL
)
table|sqlite_sequence|sqlite_sequence|3|CREATE TABLE sqlite_sequence(name,seq)
table|USER|USER|4|CREATE TABLE "USER"(
    "ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "USERNAME" TEXT NOT NULL UNIQUE,
    "PASSWORD" TEXT NOT NULL,
    "CREATED_AT" INTEGER NOT NULL DEFAULT (unixepoch())
)
index|sqlite_autoindex_USER_1|USER|5|
trigger|TEST_TRIGGER|USER|0|CREATE TRIGGER "TEST_TRIGGER"
BEFORE INSERT ON "USER"
BEGIN
    SELECT UNIXEPOCH();
END