Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subquery value not provided in beforeUpdate #2672

Open
eywolfe opened this issue Jun 10, 2024 · 1 comment
Open

Subquery value not provided in beforeUpdate #2672

eywolfe opened this issue Jun 10, 2024 · 1 comment

Comments

@eywolfe
Copy link

eywolfe commented Jun 10, 2024

I'm trying to write a static beforeUpdate hook and noticed that when my patch call uses a subquery the value(s) are not included in the inputItems array.

Here is the relevant part of my beforeUpdate definition:

  static async beforeUpdate({
    asFindQuery,
    inputItems,
    transaction,
    context,
  }: StaticHookArguments<Inventory>) {
    console.log("inputItems", inputItems);
    ...
  }

If I call the function like this then I see quantity_in_stock provided in inputItems:

  await Inventory.query(trx)
    .context({ user_profile_uuid: context.currentUserOrgProfile?.uuid })
    .whereIn('id', [...childInventories.map((i) => i.id), inventory.id])
    .patch({
      quantity_in_stock: 2,
      receiving_item_status: 'active',
    });
OUTPUT:
inputItems [
  Inventory {
    quantity_in_stock: 2,
    receiving_item_status: 'depleted',
    updated_at: '2024-06-10T17:32:22.398Z'
  }
]

However if I call the function using a subquery in the patch parameter I don't see quantity_in_stock in inputItems:

  await Inventory.query(trx)
    .context({ user_profile_uuid: context.currentUserOrgProfile?.uuid })
    .whereIn('id', [...childInventories.map((i) => i.id), inventory.id])
    .patch({
      quantity_in_stock: PickedItem.query(trx)
        .findOne({
          inventory_uuid: ref('inventory.uuid'),
          order_uuid: pickedItem.order_uuid,
        })
        .select('quantity'),
      receiving_item_status: 'active',
    });
OUTPUT:
inputItems [
  Inventory {
    receiving_item_status: 'active',
    updated_at: '2024-06-10T17:33:05.020Z'
  }
]

Is there any workaround here other than refactoring to not pass a subquery when calling patch? I can do that in this one instance, but it's not maintainable to assume we won't write code like this in the future. I also tried writing an afterUpdate function instead and ran into the same issue.

@kapouer
Copy link
Contributor

kapouer commented Aug 30, 2024

That makes sense since PickedItem.query(trx) .findOne({ inventory_uuid: ref('inventory.uuid'), order_uuid: pickedItem.order_uuid, }) .select('quantity')
returns a promise, and objection will not resolve it before calling beforeUpdate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants