2

I have model called session with composite key of three properties: seesionId, userNameId and accountId. In order to create new record I need to check if there's already session with the same userNameId and accountId.

the API of findByPk: public static async findByPk(param: number | string | Buffer, options: object): Promise

I don't understated how to pass userNameId and accountId together.

SESSION MODEL

export class Session extends Model<Session> implements ISessionModel {
    @PrimaryKey
    @Default(DataType.UUIDV4)
    @AllowNull(false)
    @Column(DataType.UUID)
    sessionId: string;

    @PrimaryKey
    @IsUUID(4)
    @AllowNull(false)
    @Column(DataType.UUID)
    accountId: string;

    @PrimaryKey
    @AllowNull(false)
    @Column
    userNameId: string;

    @AllowNull(false)
    @Column
    userName: string;
..
..
}
ker3n.nn
  • 21
  • 1
  • 2

1 Answers1

3

The docs does not specify a way to do that, but lists the ways you can find records through sequelize: (https://sequelize.org/master/manual/model-querying-finders.html)

I had to use findOne instead of findByPk

This is on plain javascript, where we try to find a record for a model with composite keys:

const res = await db.MyModel.findOne({
  where: {
    entityOneId: args.entity_one_id,
    entityTwoId: args.entity_two_id,
  }
});
rccursach
  • 305
  • 3
  • 14