1

I have a model Invoice with a BelongsToOneRelation with Subscription, so when I want to make a subscription active I run something like

await invoice.$relatedQuery('subscription', trx).update({status: "ACTIVE"})

I need to verify that there isn't any other active subscription for the same user, so I created an instance $beforeUpdate hook

  async $beforeUpdate(opt, queryContext) {
    await super.$beforeUpdate(opt, queryContext);
    if (this.status == "ACTIVE") {
      let subscriptions = await Subscription
        .query(queryContext.transaction)
        .modify('active')
        .where({user_id: this.user_id,
        .count().first()
      <pass or fail depending on the count result>
  }

The problem is that this only contains the attributes passed in update call. Is there a way to fetch all the attributes for the row that will be modified?

Ebordon
  • 31
  • 6
  • I don't think it is possible, because no queries are yet made when you are in `$beforeUpdate` hook. If you like to have values of the updated row before updating you first need to select it from DB (actually selectForUpdate would be appropriate if it is destined to be updated). – Mikael Lepistö Jun 17 '21 at 22:52

0 Answers0