Skip to content

Commit

Permalink
Allow composite key
Browse files Browse the repository at this point in the history
Fix migration to use idNames() instead of idName(), allowing for a composite primary key from multiple columns
  • Loading branch information
ataft committed Oct 5, 2021
1 parent 8bc29f7 commit 294e556
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)';
}
Expand Down

0 comments on commit 294e556

Please sign in to comment.