Skip to content

Commit f9867da

Browse files
committed
lib: Fix bug when migrating a table with foreign keys to having none
1 parent 1191c6b commit f9867da

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

lib/TableDefinition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class TableDefinition {
243243
for (columnNames in oldForeignKeys) {
244244
const columnNamesArray = columnNames.split(',');
245245

246-
if (isEqual(oldForeignKeys[columnNames], newForeignKeys[columnNames])) {
246+
if (newForeignKeys && isEqual(oldForeignKeys[columnNames], newForeignKeys[columnNames])) {
247247
if (!mustAvoidforeignKeyConflict(columnNamesArray, otherOperations)) {
248248
continue;
249249
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
const MySQLPlus = require('../../lib/MySQLPlus');
4+
5+
const config = require('../config');
6+
7+
const ColTypes = MySQLPlus.ColTypes;
8+
9+
describe('when migrating a table with foreign keys to having no foreign keys', function() {
10+
11+
const pool = MySQLPlus.createPool(config);
12+
const pool2 = MySQLPlus.createPool(config);
13+
14+
pool.defineTable('remove_all_foreign_keys_foreign', {
15+
columns: {
16+
id: ColTypes.int().notNull().primaryKey(),
17+
},
18+
});
19+
pool.defineTable('remove_all_foreign_keys_main', {
20+
columns: {
21+
id: ColTypes.int().notNull().index(),
22+
},
23+
foreignKeys: {
24+
id: 'remove_all_foreign_keys_foreign.id',
25+
},
26+
});
27+
pool2.defineTable('remove_all_foreign_keys_main', {
28+
columns: {
29+
id: ColTypes.int().notNull().index(),
30+
},
31+
});
32+
33+
before(done => {
34+
pool.sync(err => {
35+
if (err) {
36+
throw err;
37+
}
38+
39+
pool.end(done);
40+
});
41+
});
42+
43+
after(done => {
44+
pool2.end(done);
45+
});
46+
47+
it('should not error', done => {
48+
pool2.sync(done);
49+
});
50+
51+
});

0 commit comments

Comments
 (0)