Skip to content

Foreign key via REFERENCES does not work with FILESTORAGE #1846

Open

Description

@arximboldi

Example code:

CREATE TABLE IF NOT EXISTS banks (
       name STRING PRIMARY KEY,
       is_open BOOLEAN DEFAULT true,
       [key] STRING
);

CREATE TABLE IF NOT EXISTS pigs (
       id STRING PRIMARY KEY,
       bank STRING REFERENCES banks(name)
       ready BOOLEAN,
       dream STRING,
       notes STRING,
       kind STRING,
       timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);

Result:

/home/raskolnikov/dev/cerditos/server/node_modules/alasql/dist/alasql.fs.js:12518
					if (fktable.pk.columns && fktable.pk.columns.length > 0) {
					               ^

TypeError: Cannot read properties of undefined (reading 'columns')
    at /home/raskolnikov/dev/cerditos/server/node_modules/alasql/dist/alasql.fs.js:12518:21
    at Array.forEach (<anonymous>)
    at yy.CreateTable.execute (/home/raskolnikov/dev/cerditos/server/node_modules/alasql/dist/alasql.fs.js:12452:11)
    at adrunone (/home/raskolnikov/dev/cerditos/server/node_modules/alasql/dist/alasql.fs.js:4811:16)
    at FS.createTable (/home/raskolnikov/dev/cerditos/server/node_modules/alasql/dist/alasql.fs.js:18892:10)
    at yy.CreateTable.execute (/home/raskolnikov/dev/cerditos/server/node_modules/alasql/dist/alasql.fs.js:12662:38)
    at adrunone (/home/raskolnikov/dev/cerditos/server/node_modules/alasql/dist/alasql.fs.js:4811:16)
    at alasql.adrun (/home/raskolnikov/dev/cerditos/server/node_modules/alasql/dist/alasql.fs.js:4819:2)
    at alasql.dexec (/home/raskolnikov/dev/cerditos/server/node_modules/alasql/dist/alasql.fs.js:4731:11)
    at alasql.exec (/home/raskolnikov/dev/cerditos/server/node_modules/alasql/dist/alasql.fs.js:4664:17)

Node.js v18.18.2

I have tried with LOCALSTORAGE instead and the forementioned code seems to work. I don't think that foreign key rules are actually being applied though.

This alternative syntax seems to be working, but also it seems that the constraint is simply ignored:

CREATE TABLE IF NOT EXISTS pigs (
       id STRING PRIMARY KEY,
       bank STRING,
       ready BOOLEAN,
       dream STRING,
       notes STRING,
       kind STRING,
       timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
       FOREIGN KEY (bank) REFERENCES bank(name)
);

Activity

  1. leolalopeslobo commented on Mar 30, 2026

    @leolalopeslobo

    Hi, I’d like to work on this issue. Is it okay if I take it?

  2. mathiasrw commented on Apr 1, 2026

    @mathiasrw
    Member
  3. Yashmishra06 commented on May 13, 2026

    @Yashmishra06

    I'd love to help move this forward — could @mathiasrw assign this issue to me?

  4. mathiasrw commented on Sep 10, 2026

    @mathiasrw
    Member

    @Yashmishra06 Any news on this?

  5. jsingh0026 commented on Oct 21, 2026

    @jsingh0026
    Contributor

    Hi @mathiasrw 👋

    Since there had been no activity here for a while, I picked this up and opened #2798 for the crash part of this issue.

    What it fixes:

    • The TypeError: Cannot read properties of undefined (reading 'columns') on bank STRING REFERENCES banks(name) in FILESTORAGE. After ATTACH, the live tables were the same object as the schema stored in the file, so every CREATE TABLE dropped the other tables' primary keys.
    • The column in REFERENCES banks(name) was ignored, and the primary key was always used. That is why it also crashed after attaching the database again, e.g. with CREATE TABLE IF NOT EXISTS on each app start.

    What it does not fix: the constraint still isn't enforced on FILESTORAGE inserts. With autocommit on, engine inserts skip all of the table's checks. That's the cause described in #2681 / #2725, so this issue should probably stay open until that's done.

  6. added this to the v5.0.0 milestone on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions