Skip to content

Instantly share code, notes, and snippets.

@acanimal
Forked from lucasscariot/model-user.js
Last active February 17, 2020 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save acanimal/4e9967ff6118054af10b9fededb450a9 to your computer and use it in GitHub Desktop.
Save acanimal/4e9967ff6118054af10b9fededb450a9 to your computer and use it in GitHub Desktop.
Composite Primary Key in Sequelize
/*
* 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
}
@jeanlambert17
Copy link

Hi, this works fine but Sequelize is still creating an auto-increment id column in the join table.

@acanimal
Copy link
Author

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