Atomic operations: update row based on current value #1454
Answered
by
Angelelz
john-prutton
asked this question in
Q&A
-
I'm wondering if it is possible to update a row's value based on its current value. Maybe I would need to use the magic -- sql syntax
UPDATE products
SET stock = stock - 1
WHERE product_id IN (1, 2, 3, 4, 5); // drizzle syntax
await db
.update(products)
.set({ stock: products.stock - 1 }) // what I would like
.where(inArray(products.id, productIds)); |
Beta Was this translation helpful? Give feedback.
Answered by
Angelelz
Nov 2, 2023
Replies: 1 comment
-
Yes, for this type of operations you need the sql operator: await db
.update(products)
.set({ stock: sql`${products.stock} - 1` })
.where(inArray(products.id, productIds)); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
john-prutton
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, for this type of operations you need the sql operator: