From b1bf144d33cd42f37a6e602626640a7e59d2fedc Mon Sep 17 00:00:00 2001 From: ataft <11670864+ataft@users.noreply.github.com> Date: Tue, 5 Oct 2021 10:06:51 -0700 Subject: [PATCH] Allow composite key Fix migration to use idNames() instead of idName(), allowing for a composite primary key from multiple columns Signed-off-by: ataft <11670864+ataft@users.noreply.github.com> --- lib/migration.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/migration.js b/lib/migration.js index 6778526..38f0193 100644 --- a/lib/migration.js +++ b/lib/migration.js @@ -355,13 +355,15 @@ function mixinMigration(MsSQL) { // debugger; const self = this; const objModel = this._models[model]; - const modelPKID = this.idName(model); + const modelPKIDs = this.idNames(model); + let j=0; const sql = []; const props = Object.keys(objModel.properties); for (let i = 0, n = props.length; i < n; i++) { const prop = props[i]; - if (prop === modelPKID) { + if (modelPKIDs && modelPKIDs.indexOf(prop) !== -1) { + const modelPKID = modelPKIDs[j++]; const idProp = objModel.properties[modelPKID]; if (idProp.type === Number) { if (idProp.generated !== false) { @@ -387,9 +389,9 @@ function mixinMigration(MsSQL) { } let joinedSql = sql.join(',' + MsSQL.newline + ' '); let cmd = ''; - if (modelPKID) { + if (modelPKIDs && modelPKIDs.length) { cmd = 'PRIMARY KEY CLUSTERED' + MsSQL.newline + '(' + MsSQL.newline; - cmd += ' ' + self.columnEscaped(model, modelPKID) + ' ASC' + MsSQL.newline; + cmd += ' ' + modelPKIDs.map(function(modelPKID) { return self.columnEscaped(model, modelPKID) + ' ASC'; }).join(', ') + MsSQL.newline; cmd += ') WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ' + 'IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)'; }