sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

I'm not getting a way to remove an association between these two models below:

Open engmsilva opened this issue 4 years ago • 1 comments

I'm not getting a way to remove an association between these two models below:

@Table
class Ticket extends Model {
  @PrimaryKey
  @AutoIncrement
  @Column
  id: number;

  @ForeignKey(() => Queue)
  @Column
  queueId: number;

  @BelongsTo(() => Queue)
  queue: Queue;
  
}
@Table
class Queue extends Model {
  @PrimaryKey
  @AutoIncrement
  @Column
  id: number;
}

To associate a Queue to a Ticket I do the following:

const queueId = 1;
await Ticket.findByPk(ticket.id).then(async ticket => {
          await ticket.update({
            queueId
          });
ticket.reload();
  });

Any ideas on how to remove this association?

Originally posted by @engmsilva in https://github.com/RobinBuschmann/sequelize-typescript/issues/774#issuecomment-1004261081

engmsilva avatar Jan 17 '22 11:01 engmsilva

Setting their queueId to null should do it:

await ticket.update({
  queueId: null
});

ephys avatar Apr 07 '22 13:04 ephys