Skip to content

node:sqlite returning statement affects changes field in run() #57344

Open
@sant123

Description

@sant123

Version

v23.9.0

Platform

Linux jupiter 6.13.5-200.fc41.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Feb 27 15:07:31 UTC 2025 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Please run this code:

import { DatabaseSync } from "node:sqlite";
const db = new DatabaseSync(":memory:");

db.exec(`CREATE TABLE people (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  birthdate TEXT NOT NULL
) STRICT`);

const insertPeople = db.prepare(`
INSERT INTO people
  (name, birthdate)
VALUES
  (:name, :birthdate)
RETURNING id
`);

const id1 = insertPeople.run({ name: "Flash", birthdate: "1956-07-16" });
const id2 = insertPeople.run({ name: "Bruno", birthdate: "1962-12-11" });
const id3 = insertPeople.run({ name: "Bruce", birthdate: "1963-06-17" });

console.log("id1", id1);
console.log("id2", id2);
console.log("id3", id3);

How often does it reproduce? Is there a required condition?

If RETURNING id is set in the query, the changes field is affected

What is the expected behavior? Why is that the expected behavior?

id1 { lastInsertRowid: 1, changes: 1 }
id2 { lastInsertRowid: 2, changes: 1 }
id3 { lastInsertRowid: 3, changes: 1 }

What do you see instead?

id1 { lastInsertRowid: 1, changes: 0 }
id2 { lastInsertRowid: 2, changes: 1 }
id3 { lastInsertRowid: 3, changes: 1 }

In id1, the changes field should be 1

Additional information

Using better-sqlite3 correctly handles it:

import Database from "better-sqlite3";
const db = new Database(":memory:");

Outputs:

id1 { changes: 1, lastInsertRowid: 1 }
id2 { changes: 1, lastInsertRowid: 2 }
id3 { changes: 1, lastInsertRowid: 3 }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    sqliteIssues and PRs related to the SQLite subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions