F
F
frontendo2017-08-26 19:40:18
Node.js
frontendo, 2017-08-26 19:40:18

How to create composite primary key in sequelize nodejs?

const UserRole = sequelize.define(
  'user_role',
  {
    user_id: {
      type: Sequelize.INTEGER,
      allowNull: false,
      references: {
        model: User,
        key: 'id'
      }
    },
    role_id: {
      type: Sequelize.INTEGER,
      allowNull: false,
      references: {
        model: Role,
        key: 'id'
      }
    }
  },
  {
    identifier: false,
    timestamps: false,
    indexes: [
      {
        primaryKey: true,
        fields: ['user_id', 'role_id']
      }
    ]
  }
);

the meaning is visible in the code, you need to make a primary key in two columns, I did not find such an example in the documentation

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artur Aralin, 2017-08-28
@ArturAralin

Try this https://github.com/sequelize/sequelize/issues/1485...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question