From d461d07f34ce954ed108c842be8eef1f0f0b712a Mon Sep 17 00:00:00 2001 From: Matthew Dickinson Date: Fri, 30 Sep 2016 16:36:24 -0400 Subject: [PATCH] Limit foreign key creation current connector Updated fk generation to make sure that it doesn't try to create foreign keys linking to another DB/connector --- lib/migration.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/migration.js b/lib/migration.js index 060a271..5c10a3f 100644 --- a/lib/migration.js +++ b/lib/migration.js @@ -418,15 +418,19 @@ function mixinMigration(MySQL, mysql) { //TODO validate that these are in the same DB, etc. //TODO this is probably a better place to check this than in autoupdate or buildColumnDefinitions var definition = this.getModelDefinition(model); + var fk = definition.settings.foreignKeys[keyName]; if (fk) { - var fkEntity = typeof fk.entity === 'object' ? fk.entity : {name: fk.entity}; //this.getModelDefinition(fk.entity); + //get the definition of the referenced object + var fkEntityName = (typeof fk.entity === 'object') ? fk.entity.name : fk.entity; - //TODO verify that the other model in the same DB - return ' CONSTRAINT ' + this.client.escapeId(fk.name) + - ' FOREIGN KEY (' + fk.foreignKey + ')' + - ' REFERENCES ' + this.tableEscaped(fkEntity.name) + - '(' + this.client.escapeId(fk.entityKey) + ')'; + //verify that the other model in the same DB + if (this._models[fkEntityName]) { + return ' CONSTRAINT ' + this.client.escapeId(fk.name) + + ' FOREIGN KEY (' + fk.foreignKey + ')' + + ' REFERENCES ' + this.tableEscaped(fkEntityName) + + '(' + this.client.escapeId(fk.entityKey) + ')'; + } } return ''; };