Skip to content

Commit 80d555c

Browse files
authored
Merge pull request #236 from ataft/composite-key
allows composite keys on migration only
2 parents 8bc29f7 + 3852180 commit 80d555c

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

lib/migration.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,15 @@ function mixinMigration(MsSQL) {
355355
// debugger;
356356
const self = this;
357357
const objModel = this._models[model];
358-
const modelPKID = this.idName(model);
358+
const modelPKIDs = this.idNames(model);
359+
let j=0;
359360

360361
const sql = [];
361362
const props = Object.keys(objModel.properties);
362363
for (let i = 0, n = props.length; i < n; i++) {
363364
const prop = props[i];
364-
if (prop === modelPKID) {
365+
if (modelPKIDs && modelPKIDs.indexOf(prop) !== -1) {
366+
const modelPKID = modelPKIDs[j++];
365367
const idProp = objModel.properties[modelPKID];
366368
if (idProp.type === Number) {
367369
if (idProp.generated !== false) {
@@ -387,9 +389,9 @@ function mixinMigration(MsSQL) {
387389
}
388390
let joinedSql = sql.join(',' + MsSQL.newline + ' ');
389391
let cmd = '';
390-
if (modelPKID) {
392+
if (modelPKIDs && modelPKIDs.length) {
391393
cmd = 'PRIMARY KEY CLUSTERED' + MsSQL.newline + '(' + MsSQL.newline;
392-
cmd += ' ' + self.columnEscaped(model, modelPKID) + ' ASC' + MsSQL.newline;
394+
cmd += ' ' + modelPKIDs.map(function(modelPKID) { return self.columnEscaped(model, modelPKID) + ' ASC'; }).join(', ') + MsSQL.newline;
393395
cmd += ') WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ' +
394396
'IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)';
395397
}

test/id.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,61 @@ describe('Manipulating id column', function() {
128128
});
129129
});
130130

131+
it('should create composite key', function(done) {
132+
const schema =
133+
{
134+
name: 'CompositeKeyTest',
135+
options: {
136+
idInjection: false,
137+
mssql: {
138+
schema: 'dbo',
139+
table: 'COMPOSITE_KEY_TEST',
140+
},
141+
},
142+
properties: {
143+
idOne: {
144+
type: 'Number',
145+
id: true,
146+
generated: false,
147+
},
148+
idTwo: {
149+
type: 'Number',
150+
id: true,
151+
generated: false,
152+
},
153+
name: {
154+
type: 'String',
155+
required: false,
156+
length: 40,
157+
},
158+
},
159+
};
160+
161+
const models = ds.modelBuilder.buildModels(schema);
162+
const Model = models.CompositeKeyTest;
163+
Model.attachTo(ds);
164+
165+
ds.automigrate(function(err) {
166+
assert(!err);
167+
async.series([
168+
function(callback) {
169+
Model.destroyAll(callback);
170+
},
171+
function(callback) {
172+
Model.create({idOne: 1, idTwo: 2, name: 'w1'},
173+
callback);
174+
},
175+
function(callback) {
176+
ds.discoverPrimaryKeys('COMPOSITE_KEY_TEST', function(err, models) {
177+
if (err) return done(err);
178+
assert(models.length === 2);
179+
callback();
180+
});
181+
},
182+
], done);
183+
});
184+
});
185+
131186
it('should use bigint id', function(done) {
132187
const schema =
133188
{

0 commit comments

Comments
 (0)