Skip to content

node:sqlite does not support attached databases #30725

@roelentless

Description

@roelentless

Version: Deno 2.5.0

It works on node 24 but fails on Deno 2.5.0 (test files below):

❯ deno -A create.mjs
Databases created: main.sqlite, other.sqlite

❯ node test.mjs
Node attached rows: [ [Object: null prototype] { value: 'hello from other' } ]

❯ deno -A test.mjs
error: Uncaught (in promise) Error: too many attached databases - max 0
db.exec("ATTACH DATABASE 'other.sqlite' AS other;");
   ^

In read-only connections that rule makes sense, however, Deno is applying it to all connections in:

conn.set_limit(Limit::SQLITE_LIMIT_ATTACHED, 0)?;

fn open_db(
  state: &mut OpState,
  readonly: bool,
  location: &str,
  allow_extension: bool,
) -> Result<rusqlite::Connection, SqliteError> {
  if readonly {
    ...
    conn.set_limit(Limit::SQLITE_LIMIT_ATTACHED, 0)?;
    return Ok(conn);
  }
  ...
  conn.set_limit(Limit::SQLITE_LIMIT_ATTACHED, 0)?;     // <---- This line is the culprit
  Ok(conn)
}

test.mjs

import { DatabaseSync } from "node:sqlite";
const db = new DatabaseSync("main.sqlite");
db.exec("ATTACH DATABASE 'other.sqlite' AS other;");
const rows = db.prepare("SELECT * FROM other.test;").all();
console.log("Attached rows:", rows);

create.mjs

import { DatabaseSync } from "node:sqlite";

// main.sqlite with table main_table
const main = new DatabaseSync("main.sqlite");
main.exec("DROP TABLE IF EXISTS main_table;");
main.exec("CREATE TABLE main_table (value TEXT);");
main.prepare("INSERT INTO main_table (value) VALUES (?)").run("hello from main");
main.close();

// other.sqlite with table test
const other = new DatabaseSync("other.sqlite");
other.exec("DROP TABLE IF EXISTS test;");
other.exec("CREATE TABLE test (value TEXT);");
other.prepare("INSERT INTO test (value) VALUES (?)").run("hello from other");
other.close();

console.log("Databases created: main.sqlite, other.sqlite");

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs investigationrequires further investigation before determining if it is an issue or notnode compatnode:sqliteIssues related to the `node:sqlite` module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions