Skip to content

Commit

Permalink
lib: Fix foreign key bug introduced in d43235a
Browse files Browse the repository at this point in the history
That commit used a boolean which caused an "ADD FOREIGN KEY" statement to be created
for each foreign key in a table if at least one of them needed to be added back after
dropping it to prevent conflicts. This commit fixes the logic so that only the
specific keys that were dropped get added back.
  • Loading branch information
nwoltman committed Feb 17, 2017
1 parent 8f1e963 commit 03374e4
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/TableDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class TableDefinition {
const operations = [];
const oldForeignKeys = oldSchema.foreignKeys;
const newForeignKeys = this._schema.foreignKeys;
var mustAddBackKey = false;
const keysToAddBack = new Set();
var columnNames;

for (columnNames in oldForeignKeys) {
Expand All @@ -222,7 +222,7 @@ class TableDefinition {
if (!mustAvoidforeignKeyConflict(columnNamesArray, otherOperations)) {
continue;
}
mustAddBackKey = true;
keysToAddBack.add(columnNames);
}

const keyName = this._createKeyName(KEY_TYPES.FOREIGN, columnNamesArray);
Expand All @@ -234,7 +234,7 @@ class TableDefinition {

for (columnNames in newForeignKeys) {
const newForeignKeyData = newForeignKeys[columnNames];
if (!mustAddBackKey && isEqual(newForeignKeyData, oldForeignKeys[columnNames])) {
if (!keysToAddBack.has(columnNames) && isEqual(newForeignKeyData, oldForeignKeys[columnNames])) {
continue;
}
const keySQL = this._generateForeignKeySQL(columnNames, newForeignKeyData);
Expand Down

0 comments on commit 03374e4

Please sign in to comment.