sequelize.js - adding a column as a composite primary key

1.7k Views Asked by At

I have a table - source_rule_role with two composite primary key: ruleId, roleId.

I need to add another column - rowNum, that the new primary key will be composite of three columns: ruleId, roleId, rowNum.

I tried in my migration:

...
.then(() => { 
migration.addColumn(
    'source_rule_role',
    'rowNum',
    {
        type: DataTypes.INTEGER,
        allowNull: false,
        primaryKey: true,
        defaultValue: 1
    })
})
...

but I get a Multiple primary key defined error.

When I add the rowNum column, not as a primary key, it works.

1

There are 1 best solutions below

0
Nilanka Manoj On

try:

queryInterface.sequelize.query(
    'ALTER TABLE "source_rule_role" ADD CONSTRAINT "composite_pk" 
     PRIMARY KEY ("ruleId", "roleId",rowNum)');