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 ''; };