Sequelize | Composite Primary keys

159 views
Skip to first unread message

Hemant Joshi

unread,
Feb 23, 2020, 12:49:18 AM2/23/20
to Sequelize

Hi, 

I want my model to have composite primary key , so tweaked model as follows .But i keep getting  this error -- 

Unhandled rejection SequelizeDatabaseError: multiple primary keys for table "salaryRegister" are not allowed


//define models this way
module.exports = (sequelize, DataTypes) => {
  const salaryRegister = sequelize.define('salaryRegister', {
     
      employee_id: {
        type: DataTypes.STRING,
        allowNull: true
      },
      legal_entity_id: {
        type: DataTypes.STRING,
        allowNull: true
      },
      financial_year: {
        type: DataTypes.STRING,
        allowNull: true
      },
      previous_employer: {
        type: DataTypes.JSONB,
        allowNull: true
      },
      master_salary: {
        type: DataTypes.JSON,
        escape : false,
        allowNull: true
      },
    }, {
      freezeTableName: true,
      tableName: 'salaryRegister' })

      sequelize.queryInterface.addConstraint('salaryRegister', ['employee_id','legal_entity_id','financial_year'], {
        type: 'primary key',
        name: 'salaryRegister_pkey'
      })

     return salaryRegister;
  }
  
Can anyone suggest and help here .

Thanks,
Hemant

Reply all
Reply to author
Forward
0 new messages