From e25ac3420b061ce4dd9d98ef530e663e2f518d21 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 17 Dec 2025 12:52:46 +0000 Subject: [PATCH 1/8] Initial plan From ac1ba4b5356e80dd0cb817578356956a1351f59b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 17 Dec 2025 12:59:24 +0000 Subject: [PATCH 2/8] Fix foreign key and primary key detection in table columns Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> --- src/60createtable.js | 22 ++++ test/test_fk_column_detection.js | 200 +++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 test/test_fk_column_detection.js diff --git a/src/60createtable.js b/src/60createtable.js index cfdc53b114..4329590be3 100755 --- a/src/60createtable.js +++ b/src/60createtable.js @@ -166,6 +166,11 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) { throw new Error('FOREIGN KEY allowed only to tables with PRIMARY KEYs'); } } + // Store foreignkey property in column for later access + newcol.foreignkey = { + tableid: fk.tableid, + columnid: fk.columnid, + }; var fkfn = function (r) { var rr = {}; // Allow NULL values in foreign keys (check for undefined, null, and NaN) @@ -215,6 +220,12 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) { pk.onrightfn = new Function('r', 'var y;return ' + pk.onrightfns); pk.hh = hash(pk.onrightfns); table.uniqs[pk.hh] = {}; + // Mark columns with primarykey property + pk.columns.forEach(function (columnid) { + if (table.xcolumns[columnid]) { + table.xcolumns[columnid].primarykey = true; + } + }); } else if (con.type === 'CHECK') { checkfn = new Function('r,params,alasql', 'var y;return ' + con.expression.toJS('r', '')); } else if (con.type === 'UNIQUE') { @@ -247,6 +258,17 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) { throw new Error('Invalid foreign key on table ' + table.tableid); } + // Mark columns with foreignkey property + fk.columns.forEach(function (columnid, i) { + if (table.xcolumns[columnid]) { + table.xcolumns[columnid].foreignkey = { + tableid: fk.tableid, + columnid: fk.fkcolumns[i], + constraintid: con.constraintid, + }; + } + }); + checkfn = function (r) { var rr = {}; diff --git a/test/test_fk_column_detection.js b/test/test_fk_column_detection.js new file mode 100644 index 0000000000..8929b20987 --- /dev/null +++ b/test/test_fk_column_detection.js @@ -0,0 +1,200 @@ +if (typeof exports === 'object') { + var assert = require('assert'); + var alasql = require('..'); +} + +describe('Test Foreign Key and Primary Key Column Detection', function () { + const test = 'fk_column_detection'; + + before(function () { + alasql('create database test' + test); + alasql('use test' + test); + }); + + after(function () { + alasql('drop database test' + test); + }); + + it('A) Primary key constraint should mark columns with primarykey property', function () { + alasql('DROP TABLE IF EXISTS Table1'); + alasql(`CREATE TABLE Table1 ( + Column1 NUMERIC(2,0) NOT NULL, + Column2 VARCHAR(50) NOT NULL, + CONSTRAINT PK_Table1 PRIMARY KEY (Column1) + )`); + + var db = alasql.databases['test' + test]; + var table1 = db.tables.Table1; + + // Check that primary key is set on the table + assert(table1.pk, 'Table should have pk property'); + assert.deepEqual(table1.pk.columns, ['Column1']); + + // Check that Column1 is marked with primarykey property + assert(table1.xcolumns.Column1.primarykey, 'Column1 should have primarykey property set'); + }); + + it('B) Foreign key constraint should mark columns with foreignkey property', function () { + alasql('DROP TABLE IF EXISTS Table2'); + alasql('DROP TABLE IF EXISTS Table1'); + + alasql(`CREATE TABLE Table1 ( + Column1 NUMERIC(2,0) NOT NULL, + Column2 VARCHAR(50) NOT NULL, + CONSTRAINT PK_Table1 PRIMARY KEY (Column1) + )`); + + alasql(`CREATE TABLE Table2 ( + Column1 NUMERIC(2,0) NOT NULL, + Column2 NUMERIC(2,0) NOT NULL, + CONSTRAINT PK_Table2 PRIMARY KEY (Column1, Column2), + CONSTRAINT FK_Table2_Column1 FOREIGN KEY (Column1) REFERENCES Table1(Column1) + )`); + + var db = alasql.databases['test' + test]; + var table2 = db.tables.Table2; + + // Check that foreign key constraint exists in checks + var fkCheck = table2.checks.find(function (c) { + return c.fk && c.id === 'FK_Table2_Column1'; + }); + assert(fkCheck, 'Foreign key constraint should exist in checks'); + + // Check that Column1 is marked with foreignkey property + assert(table2.xcolumns.Column1.foreignkey, 'Column1 should have foreignkey property set'); + + // Verify the foreignkey property contains the correct information + var fk = table2.xcolumns.Column1.foreignkey; + assert.equal(fk.tableid, 'Table1', 'Foreign key should reference Table1'); + assert.equal(fk.columnid, 'Column1', 'Foreign key should reference Column1'); + assert.equal(fk.constraintid, 'FK_Table2_Column1', 'Foreign key should have correct constraint name'); + }); + + it('C) Multiple foreign keys on different columns should all be marked', function () { + alasql('DROP TABLE IF EXISTS Table3'); + alasql('DROP TABLE IF EXISTS TableA'); + alasql('DROP TABLE IF EXISTS TableB'); + + alasql(`CREATE TABLE TableA ( + IdA INT PRIMARY KEY + )`); + + alasql(`CREATE TABLE TableB ( + IdB INT PRIMARY KEY + )`); + + alasql(`CREATE TABLE Table3 ( + RefA INT, + RefB INT, + CONSTRAINT FK_Table3_A FOREIGN KEY (RefA) REFERENCES TableA(IdA), + CONSTRAINT FK_Table3_B FOREIGN KEY (RefB) REFERENCES TableB(IdB) + )`); + + var db = alasql.databases['test' + test]; + var table3 = db.tables.Table3; + + // Check that both columns have foreignkey property + assert(table3.xcolumns.RefA.foreignkey, 'RefA should have foreignkey property'); + assert(table3.xcolumns.RefB.foreignkey, 'RefB should have foreignkey property'); + + // Verify correct references + assert.equal(table3.xcolumns.RefA.foreignkey.tableid, 'TableA'); + assert.equal(table3.xcolumns.RefA.foreignkey.columnid, 'IdA'); + assert.equal(table3.xcolumns.RefB.foreignkey.tableid, 'TableB'); + assert.equal(table3.xcolumns.RefB.foreignkey.columnid, 'IdB'); + }); + + it('D) Composite primary key should mark all columns', function () { + alasql('DROP TABLE IF EXISTS TableComposite'); + alasql(`CREATE TABLE TableComposite ( + Key1 INT, + Key2 INT, + Data VARCHAR(50), + CONSTRAINT PK_Composite PRIMARY KEY (Key1, Key2) + )`); + + var db = alasql.databases['test' + test]; + var table = db.tables.TableComposite; + + // Both columns should be marked with primarykey property + assert(table.xcolumns.Key1.primarykey, 'Key1 should have primarykey property'); + assert(table.xcolumns.Key2.primarykey, 'Key2 should have primarykey property'); + assert(!table.xcolumns.Data.primarykey, 'Data should not have primarykey property'); + }); + + it('E) Inline foreign key should still work', function () { + alasql('DROP TABLE IF EXISTS TableInline'); + alasql('DROP TABLE IF EXISTS TableRef'); + + alasql(`CREATE TABLE TableRef ( + Id INT PRIMARY KEY + )`); + + alasql(`CREATE TABLE TableInline ( + RefId INT FOREIGN KEY REFERENCES TableRef(Id) + )`); + + var db = alasql.databases['test' + test]; + var table = db.tables.TableInline; + + // Inline foreign key should set the foreignkey property + assert(table.xcolumns.RefId.foreignkey, 'RefId should have foreignkey property from inline definition'); + }); + + it('F) Issue example - should detect foreign key in Table2.Column1', function () { + alasql('DROP TABLE IF EXISTS Table2'); + alasql('DROP TABLE IF EXISTS Table1'); + + alasql(`CREATE TABLE Table1 ( + Column1 NUMERIC(2,0) NOT NULL, + Column2 VARCHAR(50) NOT NULL, + Column3 DATETIME NOT NULL, + Column4 DATETIME NOT NULL, + Column5 VARCHAR(50) NOT NULL, + Column6 DATETIME NOT NULL, + CONSTRAINT PK_Table1 PRIMARY KEY (Column1), + CONSTRAINT UC_Column1 UNIQUE (Column1), + CONSTRAINT UC_Column3_Column4 UNIQUE (Column3, Column4) + )`); + + alasql(`CREATE TABLE Table2 ( + Column1 NUMERIC(2,0) NOT NULL, + Column2 NUMERIC(2,0) NOT NULL, + Column3 VARCHAR(300) NOT NULL, + Column4 VARCHAR(50) NOT NULL, + Column5 DATETIME NOT NULL, + Column6 NUMERIC(4,0) NOT NULL, + CONSTRAINT PK_Table2 PRIMARY KEY (Column1, Column2), + CONSTRAINT UC_PK_Table2 UNIQUE (Column1, Column2), + CONSTRAINT FK_Table2_Column1 FOREIGN KEY (Column1) REFERENCES Table1(Column1) + )`); + + var db = alasql.databases['test' + test]; + var table1 = db.tables.Table1; + var table2 = db.tables.Table2; + + // Table1.Column1 should be marked as primary key + assert( + table1.xcolumns.Column1.primarykey, + 'Table1.Column1 should have primarykey property' + ); + + // Table2 columns should be marked as primary key + assert( + table2.xcolumns.Column1.primarykey, + 'Table2.Column1 should have primarykey property' + ); + assert( + table2.xcolumns.Column2.primarykey, + 'Table2.Column2 should have primarykey property' + ); + + // Table2.Column1 should be marked as foreign key + assert( + table2.xcolumns.Column1.foreignkey, + 'Table2.Column1 should have foreignkey property' + ); + assert.equal(table2.xcolumns.Column1.foreignkey.tableid, 'Table1'); + assert.equal(table2.xcolumns.Column1.foreignkey.columnid, 'Column1'); + }); +}); From 652f16cabe55e70342e34afafa1bf7d3af75504a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 17 Dec 2025 13:49:18 +0000 Subject: [PATCH 3/8] Rename test file to test1643.js per review feedback Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> --- test/{test_fk_column_detection.js => test1643.js} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename test/{test_fk_column_detection.js => test1643.js} (98%) diff --git a/test/test_fk_column_detection.js b/test/test1643.js similarity index 98% rename from test/test_fk_column_detection.js rename to test/test1643.js index 8929b20987..9df9aae81e 100644 --- a/test/test_fk_column_detection.js +++ b/test/test1643.js @@ -3,8 +3,8 @@ if (typeof exports === 'object') { var alasql = require('..'); } -describe('Test Foreign Key and Primary Key Column Detection', function () { - const test = 'fk_column_detection'; +describe('Test 1643 - Foreign Key and Primary Key Column Detection', function () { + const test = '1643'; before(function () { alasql('create database test' + test); From 4bfe3c364e8d7bbdfc2d5f0ba97a26a647596425 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 17 Dec 2025 14:00:52 +0000 Subject: [PATCH 4/8] Changes before error encountered Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> --- test/test1643.js | 148 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/test/test1643.js b/test/test1643.js index 9df9aae81e..21229264bb 100644 --- a/test/test1643.js +++ b/test/test1643.js @@ -197,4 +197,152 @@ describe('Test 1643 - Foreign Key and Primary Key Column Detection', function () assert.equal(table2.xcolumns.Column1.foreignkey.tableid, 'Table1'); assert.equal(table2.xcolumns.Column1.foreignkey.columnid, 'Column1'); }); + + it('G) Primary key should prevent duplicate insertions', function () { + alasql('DROP TABLE IF EXISTS PKTest'); + alasql(`CREATE TABLE PKTest ( + Id INT, + Name VARCHAR(50), + CONSTRAINT PK_PKTest PRIMARY KEY (Id) + )`); + + // First insert should succeed + var res1 = alasql('INSERT INTO PKTest VALUES (1, "First")'); + assert.equal(res1, 1, 'First insert should succeed'); + + // Second insert with same primary key should fail + assert.throws(function () { + alasql('INSERT INTO PKTest VALUES (1, "Duplicate")'); + }, Error, 'Duplicate primary key should throw error'); + + // Insert with different primary key should succeed + var res2 = alasql('INSERT INTO PKTest VALUES (2, "Second")'); + assert.equal(res2, 1, 'Insert with different primary key should succeed'); + }); + + it('H) Foreign key should prevent insertion of non-existent references', function () { + alasql('DROP TABLE IF EXISTS FKChild'); + alasql('DROP TABLE IF EXISTS FKParent'); + + alasql(`CREATE TABLE FKParent ( + ParentId INT PRIMARY KEY, + ParentName VARCHAR(50) + )`); + + alasql(`CREATE TABLE FKChild ( + ChildId INT PRIMARY KEY, + ParentId INT, + ChildName VARCHAR(50), + CONSTRAINT FK_Child_Parent FOREIGN KEY (ParentId) REFERENCES FKParent(ParentId) + )`); + + // Insert valid parent record + alasql('INSERT INTO FKParent VALUES (1, "Parent1")'); + alasql('INSERT INTO FKParent VALUES (2, "Parent2")'); + + // Insert child with valid foreign key should succeed + var res1 = alasql('INSERT INTO FKChild VALUES (1, 1, "Child1")'); + assert.equal(res1, 1, 'Insert with valid foreign key should succeed'); + + // Insert child with invalid foreign key should fail + assert.throws(function () { + alasql('INSERT INTO FKChild VALUES (2, 99, "Child2")'); + }, Error, 'Insert with invalid foreign key should throw error'); + + // Verify the foreignkey property is set correctly + var db = alasql.databases['test' + test]; + var childTable = db.tables.FKChild; + assert(childTable.xcolumns.ParentId.foreignkey, 'ParentId should have foreignkey property'); + assert.equal(childTable.xcolumns.ParentId.foreignkey.tableid, 'FKParent'); + assert.equal(childTable.xcolumns.ParentId.foreignkey.columnid, 'ParentId'); + }); + + it('I) Foreign key with NULL values should be allowed', function () { + alasql('DROP TABLE IF EXISTS FKNullChild'); + alasql('DROP TABLE IF EXISTS FKNullParent'); + + alasql(`CREATE TABLE FKNullParent ( + Id INT PRIMARY KEY + )`); + + alasql(`CREATE TABLE FKNullChild ( + ChildId INT PRIMARY KEY, + ParentId INT, + CONSTRAINT FK_Null_Test FOREIGN KEY (ParentId) REFERENCES FKNullParent(Id) + )`); + + alasql('INSERT INTO FKNullParent VALUES (1)'); + + // Insert with NULL foreign key should succeed (NULL is allowed) + var res = alasql('INSERT INTO FKNullChild VALUES (1, NULL)'); + assert.equal(res, 1, 'Insert with NULL foreign key should succeed'); + + // Insert with valid foreign key should succeed + var res2 = alasql('INSERT INTO FKNullChild VALUES (2, 1)'); + assert.equal(res2, 1, 'Insert with valid foreign key should succeed'); + }); + + it('J) Composite primary key should enforce uniqueness on combination', function () { + alasql('DROP TABLE IF EXISTS CompositePK'); + alasql(`CREATE TABLE CompositePK ( + Key1 INT, + Key2 INT, + Data VARCHAR(50), + CONSTRAINT PK_Composite PRIMARY KEY (Key1, Key2) + )`); + + // First insert should succeed + var res1 = alasql('INSERT INTO CompositePK VALUES (1, 1, "First")'); + assert.equal(res1, 1, 'First insert should succeed'); + + // Insert with same combination should fail + assert.throws(function () { + alasql('INSERT INTO CompositePK VALUES (1, 1, "Duplicate")'); + }, Error, 'Duplicate composite key should throw error'); + + // Insert with different Key1 but same Key2 should succeed + var res2 = alasql('INSERT INTO CompositePK VALUES (2, 1, "Different Key1")'); + assert.equal(res2, 1, 'Different Key1 with same Key2 should succeed'); + + // Insert with same Key1 but different Key2 should succeed + var res3 = alasql('INSERT INTO CompositePK VALUES (1, 2, "Different Key2")'); + assert.equal(res3, 1, 'Same Key1 with different Key2 should succeed'); + + // Verify both columns are marked as primary keys + var db = alasql.databases['test' + test]; + var table = db.tables.CompositePK; + assert(table.xcolumns.Key1.primarykey, 'Key1 should have primarykey property'); + assert(table.xcolumns.Key2.primarykey, 'Key2 should have primarykey property'); + }); + + it('K) Inline foreign key should enforce referential integrity', function () { + alasql('DROP TABLE IF EXISTS InlineChild'); + alasql('DROP TABLE IF EXISTS InlineParent'); + + alasql(`CREATE TABLE InlineParent ( + Id INT PRIMARY KEY + )`); + + alasql(`CREATE TABLE InlineChild ( + ChildId INT PRIMARY KEY, + ParentId INT FOREIGN KEY REFERENCES InlineParent(Id) + )`); + + // Insert parent record + alasql('INSERT INTO InlineParent VALUES (10)'); + + // Insert with valid foreign key should succeed + var res1 = alasql('INSERT INTO InlineChild VALUES (1, 10)'); + assert.equal(res1, 1, 'Insert with valid inline foreign key should succeed'); + + // Insert with invalid foreign key should fail + assert.throws(function () { + alasql('INSERT INTO InlineChild VALUES (2, 99)'); + }, Error, 'Insert with invalid inline foreign key should throw error'); + + // Verify foreignkey property is set + var db = alasql.databases['test' + test]; + var table = db.tables.InlineChild; + assert(table.xcolumns.ParentId.foreignkey, 'ParentId should have foreignkey property'); + }); }); From e22f735fd316c223d90bdea8e2c0bf379b7b5789 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 21 Dec 2025 04:56:02 +0000 Subject: [PATCH 5/8] Add behavioral tests for FK/PK constraints and fix NULL/NaN handling Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> --- src/60createtable.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/60createtable.js b/src/60createtable.js index 4329590be3..c5c97a30e2 100755 --- a/src/60createtable.js +++ b/src/60createtable.js @@ -180,6 +180,7 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) { val === null || (typeof val === 'number' && isNaN(val)) ) { + if (r[col.columnid] == null) { return true; } rr[fk.columnid] = val; @@ -274,8 +275,10 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) { //Composite foreign keys fk.fkcolumns.forEach(function (colFk, i) { - if (r[fk.columns[i]] != null) { - rr[colFk] = r[fk.columns[i]]; + var val = r[fk.columns[i]]; + // Only include non-null, non-undefined, non-NaN values + if (val != null && !(typeof val === 'number' && isNaN(val))) { + rr[colFk] = val; } }); From 46c03b8787194a30d4e3dc18f538583dcc5c3ff7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 21 Dec 2025 05:16:22 +0000 Subject: [PATCH 6/8] Add NaN check to inline FK and test for NULL values with inline FKs Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> --- src/60createtable.js | 10 ++++++++++ test/test1643.js | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/60createtable.js b/src/60createtable.js index c5c97a30e2..2b3d8cf41f 100755 --- a/src/60createtable.js +++ b/src/60createtable.js @@ -187,6 +187,16 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) { var addr = fktable.pk.onrightfn(rr); if (!fktable.uniqs[fktable.pk.hh][addr]) { throw new Error('Foreign key "' + val + '" not found in table "' + fk.tableid + '"'); + var val = r[col.columnid]; + // Only check foreign key if value is not null, undefined, or NaN + if (val != null && !(typeof val === 'number' && isNaN(val))) { + rr[fk.columnid] = val; + var addr = fktable.pk.onrightfn(rr); + if (!fktable.uniqs[fktable.pk.hh][addr]) { + throw new Error( + 'Foreign key "' + val + '" not found in table "' + fk.tableid + '"' + ); + } } return true; }; diff --git a/test/test1643.js b/test/test1643.js index 21229264bb..761cfc5b37 100644 --- a/test/test1643.js +++ b/test/test1643.js @@ -345,4 +345,28 @@ describe('Test 1643 - Foreign Key and Primary Key Column Detection', function () var table = db.tables.InlineChild; assert(table.xcolumns.ParentId.foreignkey, 'ParentId should have foreignkey property'); }); + + it('L) Inline foreign key should allow NULL values', function () { + alasql('DROP TABLE IF EXISTS InlineNullChild'); + alasql('DROP TABLE IF EXISTS InlineNullParent'); + + alasql(`CREATE TABLE InlineNullParent ( + Id INT PRIMARY KEY + )`); + + alasql(`CREATE TABLE InlineNullChild ( + ChildId INT PRIMARY KEY, + ParentId INT FOREIGN KEY REFERENCES InlineNullParent(Id) + )`); + + alasql('INSERT INTO InlineNullParent VALUES (1)'); + + // Insert with NULL foreign key should succeed (NULL is allowed) + var res = alasql('INSERT INTO InlineNullChild VALUES (1, NULL)'); + assert.equal(res, 1, 'Insert with NULL inline foreign key should succeed'); + + // Insert with valid foreign key should succeed + var res2 = alasql('INSERT INTO InlineNullChild VALUES (2, 1)'); + assert.equal(res2, 1, 'Insert with valid inline foreign key should succeed'); + }); }); From 84b39a2e6b4ee27884482b7fb0e0e457337a5536 Mon Sep 17 00:00:00 2001 From: "M. Wulff" Date: Mon, 22 Dec 2025 10:09:10 +1100 Subject: [PATCH 7/8] Fix merge --- src/60createtable.js | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/src/60createtable.js b/src/60createtable.js index 2b3d8cf41f..d91804e529 100755 --- a/src/60createtable.js +++ b/src/60createtable.js @@ -173,29 +173,13 @@ yy.CreateTable.prototype.execute = function (databaseid, params, cb) { }; var fkfn = function (r) { var rr = {}; - // Allow NULL values in foreign keys (check for undefined, null, and NaN) - var val = r[col.columnid]; - if ( - typeof val === 'undefined' || - val === null || - (typeof val === 'number' && isNaN(val)) - ) { - if (r[col.columnid] == null) { - return true; - } - rr[fk.columnid] = val; - var addr = fktable.pk.onrightfn(rr); - if (!fktable.uniqs[fktable.pk.hh][addr]) { - throw new Error('Foreign key "' + val + '" not found in table "' + fk.tableid + '"'); var val = r[col.columnid]; // Only check foreign key if value is not null, undefined, or NaN if (val != null && !(typeof val === 'number' && isNaN(val))) { rr[fk.columnid] = val; var addr = fktable.pk.onrightfn(rr); if (!fktable.uniqs[fktable.pk.hh][addr]) { - throw new Error( - 'Foreign key "' + val + '" not found in table "' + fk.tableid + '"' - ); + throw new Error('Foreign key "' + val + '" not found in table "' + fk.tableid + '"'); } } return true; From 90493d160a93c9884af898715b2a38dee03445ec Mon Sep 17 00:00:00 2001 From: "M. Wulff" Date: Mon, 22 Dec 2025 10:09:18 +1100 Subject: [PATCH 8/8] Clean up test output --- test/test1409.js | 2 +- test/test1643.js | 71 ++++++++++++++++++++++++++++-------------------- test/test269.js | 2 +- test/test292.js | 4 +-- test/test329.js | 8 +++--- test/test338.js | 4 +-- test/test339.js | 8 +++--- test/test340.js | 2 +- test/test407.js | 6 ++-- 9 files changed, 59 insertions(+), 48 deletions(-) diff --git a/test/test1409.js b/test/test1409.js index 751b017501..5e8276b3a1 100644 --- a/test/test1409.js +++ b/test/test1409.js @@ -14,7 +14,7 @@ if (typeof exports != 'object') { var count = 0; alasql.fn.onInsert = function (r) { count++; - console.log('this never happens!'); + // console.log('this never happens!'); }; return alasql diff --git a/test/test1643.js b/test/test1643.js index 761cfc5b37..9747f4f13d 100644 --- a/test/test1643.js +++ b/test/test1643.js @@ -67,7 +67,11 @@ describe('Test 1643 - Foreign Key and Primary Key Column Detection', function () var fk = table2.xcolumns.Column1.foreignkey; assert.equal(fk.tableid, 'Table1', 'Foreign key should reference Table1'); assert.equal(fk.columnid, 'Column1', 'Foreign key should reference Column1'); - assert.equal(fk.constraintid, 'FK_Table2_Column1', 'Foreign key should have correct constraint name'); + assert.equal( + fk.constraintid, + 'FK_Table2_Column1', + 'Foreign key should have correct constraint name' + ); }); it('C) Multiple foreign keys on different columns should all be marked', function () { @@ -138,7 +142,10 @@ describe('Test 1643 - Foreign Key and Primary Key Column Detection', function () var table = db.tables.TableInline; // Inline foreign key should set the foreignkey property - assert(table.xcolumns.RefId.foreignkey, 'RefId should have foreignkey property from inline definition'); + assert( + table.xcolumns.RefId.foreignkey, + 'RefId should have foreignkey property from inline definition' + ); }); it('F) Issue example - should detect foreign key in Table2.Column1', function () { @@ -174,26 +181,14 @@ describe('Test 1643 - Foreign Key and Primary Key Column Detection', function () var table2 = db.tables.Table2; // Table1.Column1 should be marked as primary key - assert( - table1.xcolumns.Column1.primarykey, - 'Table1.Column1 should have primarykey property' - ); + assert(table1.xcolumns.Column1.primarykey, 'Table1.Column1 should have primarykey property'); // Table2 columns should be marked as primary key - assert( - table2.xcolumns.Column1.primarykey, - 'Table2.Column1 should have primarykey property' - ); - assert( - table2.xcolumns.Column2.primarykey, - 'Table2.Column2 should have primarykey property' - ); + assert(table2.xcolumns.Column1.primarykey, 'Table2.Column1 should have primarykey property'); + assert(table2.xcolumns.Column2.primarykey, 'Table2.Column2 should have primarykey property'); // Table2.Column1 should be marked as foreign key - assert( - table2.xcolumns.Column1.foreignkey, - 'Table2.Column1 should have foreignkey property' - ); + assert(table2.xcolumns.Column1.foreignkey, 'Table2.Column1 should have foreignkey property'); assert.equal(table2.xcolumns.Column1.foreignkey.tableid, 'Table1'); assert.equal(table2.xcolumns.Column1.foreignkey.columnid, 'Column1'); }); @@ -211,9 +206,13 @@ describe('Test 1643 - Foreign Key and Primary Key Column Detection', function () assert.equal(res1, 1, 'First insert should succeed'); // Second insert with same primary key should fail - assert.throws(function () { - alasql('INSERT INTO PKTest VALUES (1, "Duplicate")'); - }, Error, 'Duplicate primary key should throw error'); + assert.throws( + function () { + alasql('INSERT INTO PKTest VALUES (1, "Duplicate")'); + }, + Error, + 'Duplicate primary key should throw error' + ); // Insert with different primary key should succeed var res2 = alasql('INSERT INTO PKTest VALUES (2, "Second")'); @@ -245,9 +244,13 @@ describe('Test 1643 - Foreign Key and Primary Key Column Detection', function () assert.equal(res1, 1, 'Insert with valid foreign key should succeed'); // Insert child with invalid foreign key should fail - assert.throws(function () { - alasql('INSERT INTO FKChild VALUES (2, 99, "Child2")'); - }, Error, 'Insert with invalid foreign key should throw error'); + assert.throws( + function () { + alasql('INSERT INTO FKChild VALUES (2, 99, "Child2")'); + }, + Error, + 'Insert with invalid foreign key should throw error' + ); // Verify the foreignkey property is set correctly var db = alasql.databases['test' + test]; @@ -296,9 +299,13 @@ describe('Test 1643 - Foreign Key and Primary Key Column Detection', function () assert.equal(res1, 1, 'First insert should succeed'); // Insert with same combination should fail - assert.throws(function () { - alasql('INSERT INTO CompositePK VALUES (1, 1, "Duplicate")'); - }, Error, 'Duplicate composite key should throw error'); + assert.throws( + function () { + alasql('INSERT INTO CompositePK VALUES (1, 1, "Duplicate")'); + }, + Error, + 'Duplicate composite key should throw error' + ); // Insert with different Key1 but same Key2 should succeed var res2 = alasql('INSERT INTO CompositePK VALUES (2, 1, "Different Key1")'); @@ -336,9 +343,13 @@ describe('Test 1643 - Foreign Key and Primary Key Column Detection', function () assert.equal(res1, 1, 'Insert with valid inline foreign key should succeed'); // Insert with invalid foreign key should fail - assert.throws(function () { - alasql('INSERT INTO InlineChild VALUES (2, 99)'); - }, Error, 'Insert with invalid inline foreign key should throw error'); + assert.throws( + function () { + alasql('INSERT INTO InlineChild VALUES (2, 99)'); + }, + Error, + 'Insert with invalid inline foreign key should throw error' + ); // Verify foreignkey property is set var db = alasql.databases['test' + test]; diff --git a/test/test269.js b/test/test269.js index ab61a7f183..28a2d14a6c 100644 --- a/test/test269.js +++ b/test/test269.js @@ -90,7 +90,7 @@ describe('Test 269 options', function () { ORDER BY a', [data1, data2] ); - console.log(res); + // console.log(res); // Wrong with reduced rows assert.deepEqual(res, [ [undefined, 40, 400], diff --git a/test/test292.js b/test/test292.js index c5e34806c7..b107f042f0 100644 --- a/test/test292.js +++ b/test/test292.js @@ -15,13 +15,13 @@ describe('Test 292 Nested searches', function () { it.skip('2. Search inside select', function (done) { var res = alasql('SELECT (SEARCH b SUM(/c) FROM _) FROM ?', [data]); - console.log(res); + // console.log(res); done(); }); it.skip('3. SELECT inside SEARCH', function (done) { var res = alasql('SEARCH a (SELECT SUM(c) FROM b) FROM ?'); - console.log(res); + // console.log(res); done(); }); diff --git a/test/test329.js b/test/test329.js index 446d5c86f9..24b0165a3f 100644 --- a/test/test329.js +++ b/test/test329.js @@ -14,26 +14,26 @@ describe('Test 329 PROLOG', function () { it.skip('2. FACTS', function (done) { var res = alasql('CREATE GRAPH Alex > son > Michael'); var res = alasql(':- son(Alex,Larissa)'); - console.log(res); + // console.log(res); done(); }); it.skip('3. RULES', function (done) { var res = alasql('son(@x,@y) :- parent(@y,@x)'); - console.log(res); + // console.log(res); done(); }); it.skip('4. QUERY', function (done) { var res = alasql('?- parent(@x,Alex)'); var res = alasql('?- @x>parent>Alex)'); - console.log(res); + // console.log(res); done(); }); it.skip('5. Expression statement', function (done) { var res = alasql('= 100+1'); - console.log(res); + // console.log(res); done(); }); diff --git a/test/test338.js b/test/test338.js index bd6cececba..2e916166e6 100644 --- a/test/test338.js +++ b/test/test338.js @@ -48,7 +48,7 @@ select top 3 b.col from b order by b.col desc; */ }); - console.log(res); + // console.log(res); // assert.deepEqual(res,1); done(); }); @@ -67,7 +67,7 @@ select col from cte_for_b; */ }); - console.log(res); + // console.log(res); // assert.deepEqual(res,1); done(); }); diff --git a/test/test339.js b/test/test339.js index 177cfb83c5..95e3e7da88 100644 --- a/test/test339.js +++ b/test/test339.js @@ -121,7 +121,7 @@ insert into c (col) values (1), (2), (5); select col from c; */ }); - console.log(res); + // console.log(res); assert.deepEqual(res.sort(), [1, 2, 3, 4]); done(); }); @@ -136,7 +136,7 @@ insert into c (col) values (1), (2), (5); select col from c; */ }); - console.log(res); + // console.log(res); assert.deepEqual(res.sort(), [3]); done(); }); @@ -151,7 +151,7 @@ insert into c (col) values (1), (2), (5); select col from c; */ }); - console.log(res); + // console.log(res); assert.deepEqual(res.sort(), [1, 2, 3]); done(); }); @@ -166,7 +166,7 @@ insert into c (col) values (1), (2), (5); select col from c; */ }); - console.log(res); + // console.log(res); assert.deepEqual(res.sort(), [1, 2]); done(); }); diff --git a/test/test340.js b/test/test340.js index 191d8de63c..d0fd81e00f 100644 --- a/test/test340.js +++ b/test/test340.js @@ -26,7 +26,7 @@ describe('Test 340 SET PARAMS', function () { */ }); - console.log(res); + // console.log(res); assert.deepEqual(res, [1, 'bar']); done(); diff --git a/test/test407.js b/test/test407.js index 00938042f5..ab7922ec49 100644 --- a/test/test407.js +++ b/test/test407.js @@ -102,7 +102,7 @@ describe('Test 407 - TWO JOINS', function () { var res = alasql( 'SELECT one.id AS a, two.id AS b, three.id AS c FROM one LEFT JOIN two ON one.id = two.id RIGHT JOIN three ON two.id = three.id' ); - console.log(res); + // console.log(res); assert.deepEqual(res, [ [undefined, undefined, 'C'], [undefined, undefined, 'BC'], @@ -116,7 +116,7 @@ describe('Test 407 - TWO JOINS', function () { var res = alasql( 'SELECT one.id AS a, two.id AS b, three.id AS c FROM one LEFT JOIN two ON one.id = two.id OUTER JOIN three ON two.id = three.id' ); - console.log(res); + // console.log(res); assert.deepEqual(res, [ ['A', undefined, undefined], ['AB', 'AB', undefined], @@ -170,7 +170,7 @@ describe('Test 407 - TWO JOINS', function () { var res = alasql( 'SELECT one.id AS a, two.id AS b, three.id AS c FROM one RIGHT JOIN two ON one.id = two.id OUTER JOIN three ON two.id = three.id' ); - console.log(res); + // console.log(res); assert.deepEqual(res, [ [undefined, 'B', undefined], ['AB', 'AB', undefined],