Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions src/60createtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,21 @@ 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)
var val = r[col.columnid];
if (
typeof val === 'undefined' ||
val === null ||
(typeof val === 'number' && isNaN(val))
) {
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 + '"');
// 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;
};
Expand Down Expand Up @@ -215,6 +215,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') {
Expand Down Expand Up @@ -247,13 +253,26 @@ 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 = {};

//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;
}
});

Expand Down
2 changes: 1 addition & 1 deletion test/test1409.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading