-
-
Save acanimal/4e9967ff6118054af10b9fededb450a9 to your computer and use it in GitHub Desktop.
Composite Primary Key in Sequelize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Migration | |
*/ | |
module.exports = { | |
up: function (queryInterface, Sequelize) { | |
return queryInterface.createTable('JobSkills', { | |
experience: { | |
type: Sequelize.INTEGER, | |
allowNull: false | |
}, | |
createdAt: { | |
type: Sequelize.DATE, | |
allowNull: false | |
}, | |
updatedAt: { | |
type: Sequelize.DATE, | |
allowNull: false | |
}, | |
// Associations | |
JobId: { | |
type: Sequelize.INTEGER, | |
allowNull: false, | |
onDelete: 'cascade', | |
references: { | |
model: 'Jobs', | |
key: 'id' | |
} | |
}, | |
SkillId: { | |
type: Sequelize.INTEGER, | |
allowNull: false, | |
references: { | |
model: 'Skills', | |
key: 'id' | |
} | |
} | |
}) | |
.then(() => queryInterface.addConstraint('JobSkills', ['JobId', 'SkillId'], { | |
type: 'primary key', | |
name: 'JobSkills_pkey' | |
})) | |
}, | |
down: function (queryInterface, Sequelize) { | |
return queryInterface.dropTable('JobSkills') | |
} | |
} | |
/* | |
* Model | |
*/ | |
module.exports = (sequelize, DataTypes) => { | |
const JobSkill = sequelize.define('JobSkill', { | |
experience: { | |
type: DataTypes.INTEGER, | |
allowNull: false | |
}, | |
createdAt: { | |
type: DataTypes.DATE, | |
allowNull: false | |
}, | |
updatedAt: { | |
type: DataTypes.DATE, | |
allowNull: false | |
} | |
}) | |
return JobSkill | |
} | |
Yes, and not sure what is the best approach. A possible solucion is through queryInterface
adding new "constrains" and removing the old one.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, this works fine but Sequelize is still creating an auto-increment id column in the join table.