For anyone who wants to create a primary key of a composite index based on the columns (keys) in your join table when performing migrations. You will need to add a primary key constraint for the two columns that you want to use as the combined primary key for the table.
module.exports = { up: function (queryInterface, Sequelize) { return queryInterface.createTable('itemtags', { itemId: { type: Sequelize.INTEGER, references: { model: 'items', key: 'id', }, onDelete: 'CASCADE', onUpdate: 'CASCADE', allowNull: false }, tagId: { type: Sequelize.INTEGER, references: { model: 'tags', key: 'id', }, onDelete: 'CASCADE', onUpdate: 'CASCADE', allowNull: false } }) .then(() => { return queryInterface.addConstraint('itemtags', ['itemId', 'tagId'], { type: 'primary key', name: 'gametag_pkey' }); }); }, down: function (queryInterface, Sequelize) { return queryInterface.dropTable('gametags'); } };
This is about the same as when ALTER TABLE ONLY my_table ADD CONSTRAINT pk_my_table PRIMARY KEY(column1,column2); in postgres.
holmberd
source share