Skip to content

Commit fa8960b

Browse files
committed
lib: Fix bug when migrating a table with no foreign keys to having some
1 parent 941c301 commit fa8960b

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

lib/TableDefinition.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const Operation = require('./Operation');
55

66
const cloneDeep = require('lodash/cloneDeep');
77
const diffKeys = require('./utils/diffKeys');
8-
const isEmpty = require('lodash/isEmpty');
98
const isEqual = require('lodash/isEqual');
109

1110
const KEY_TYPES = {
@@ -753,7 +752,7 @@ function generateForegnKeysSchema(createDefinitions) {
753752
};
754753
}
755754

756-
return isEmpty(foreignKeys) ? null : foreignKeys;
755+
return foreignKeys;
757756
}
758757

759758
function arraysEqual(a, b) {

test/integration/addForeignKeys.js

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 no foreign keys to having some foreign keys', function() {
10+
11+
const pool = MySQLPlus.createPool(config);
12+
const pool2 = MySQLPlus.createPool(config);
13+
14+
pool.defineTable('add_foreign_keys_foreign', {
15+
columns: {
16+
id: ColTypes.int().notNull().primaryKey(),
17+
},
18+
});
19+
pool.defineTable('add_foreign_keys_main', {
20+
columns: {
21+
id: ColTypes.int().notNull().index(),
22+
},
23+
});
24+
pool2.defineTable('add_foreign_keys_main', {
25+
columns: {
26+
id: ColTypes.int().notNull().index(),
27+
},
28+
foreignKeys: {
29+
id: 'add_foreign_keys_foreign.id',
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)