From 4d2b0a76a5fae3d175cf9aaaabd1cf7f5b2ffe21 Mon Sep 17 00:00:00 2001 From: MarkMYoung Date: Wed, 18 Apr 2012 16:10:03 -0500 Subject: [PATCH] Added foreign key existence check for prefetched "hasOne" relationships in 'list' function. It throws a standard ReferenceError exception in case a typo was made. --- lib/persistence.store.sql.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/persistence.store.sql.js b/lib/persistence.store.sql.js index 07b681f..bdb7a2e 100644 --- a/lib/persistence.store.sql.js +++ b/lib/persistence.store.sql.js @@ -648,8 +648,10 @@ function config(persistence, dialect) { joinSql += this._additionalJoinSqls.join(' '); - for ( var i = 0; i < this._prefetchFields.length; i++) { - var prefetchField = this._prefetchFields[i]; + this._prefetchFields.forEach(function(prefetchField) { + if(!(prefetchField in meta.hasOne)) { + throw(new ReferenceError( "'" + meta.name + "' does not 'hasOne' field called '" + prefetchField + "'.")); + } var thisMeta = meta.hasOne[prefetchField].type.meta; if (thisMeta.isMixin) throw new Error("cannot prefetch a mixin"); @@ -659,7 +661,7 @@ function config(persistence, dialect) { joinSql += "LEFT JOIN `" + thisMeta.name + "` AS `" + tableAlias + "` ON `" + tableAlias + "`.`id` = `" + mainAlias + '`.`' + prefetchField + "` "; - } + }); var whereSql = "WHERE " + [ this._filter.sql(meta, mainAlias, args) ].concat(additionalWhereSqls).join(' AND ');