oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.91k stars 2.74k forks source link

SQLite: Incorrect foreign key if column names match #6837

Open JamesAlexanderHill opened 11 months ago

JamesAlexanderHill commented 11 months ago

What version of Bun is running?

1.0.7+b0393fba6200d8573f3433fb0af258a0e33ac157

What platform is your computer?

Darwin 21.6.0 x86_64 i386

What steps can reproduce the bug?

run the following in a script and view the file generated in SQL Viewer.

import { Database } from "bun:sqlite";
const db = new Database("database.sqlite");

const CREATE_TABLE_A = db.prepare(
  `CREATE TABLE IF NOT EXISTS T_A (
    id TEXT PRIMARY KEY,
    otherValue TEXT NOT NULL UNIQUE,
    altValue BOOLEAN
  );`,
);
CREATE_TABLE_A.run();

const CREATE_TABLE_B = db.prepare(
  `CREATE TABLE IF NOT EXISTS T_B (
    id TEXT PRIMARY KEY,
    aId TEXT NOT NULL,
    stuff BOOLEAN NOT NULL DEFAULT false,
    CONSTRAINT fk_aId FOREIGN KEY (aId) REFERENCES T_A (id)
  );`,
);
CREATE_TABLE_B.run();

What is the expected behavior?

Expected behaviour:

What do you see instead?

T_B will have column id marked as a primary and foreign key.

Additional information

No response

Hanaasagi commented 11 months ago
CREATE TABLE IF NOT EXISTS T_A (
    id TEXT PRIMARY KEY,
    otherValue TEXT NOT NULL UNIQUE,
    altValue BOOLEAN
  );

CREATE TABLE IF NOT EXISTS T_B (
    id TEXT PRIMARY KEY,
    aId TEXT NOT NULL,
    stuff BOOLEAN NOT NULL DEFAULT false,
    CONSTRAINT fk_aId FOREIGN KEY (aId) REFERENCES T_A (id)
  );
$ sqlite3 test.db < create_tables.sql

I use SQL file to create the database and then upload it to SQL viewer website, and I get the same result.

And in SQLite cli, I can see aId is a foreign key.

2023-11-05_21-00