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?