I want to get back a value after I insert my data
const data = {name: "Christmas Shirt 1", price: 5.99, sku: "20210911S", warehouse_id: 2}
const skuCode = await query.returning(['sku_code'], {includeTriggerModifications:true}).insert(data);
console.log(skuCode) // ["20210911-NY"];
However, the table that I’m inserting into has a AFTER INSERT
trigger that will append a prefix to the sku_code
field:
CREATE TRIGGER inventory_trigger
ON inventory AFTER TRIGGER AS
BEGIN
UPDATE t
SET sku_code = CASE t.warehouse
WHEN 1 THEN t.sku_code + "NV"
WHEN 2 THEN t.sku_code + "NY"
END
FROM inventory t
JOIN inserted i ON t.id = i.id
END
Is there a way for me to get the value after the AFTER INSERT
trigger using Knex’s returning()
? I know I can add similar logic in my JS code that manually prepends the prefix but I don’t want to duplicate the logic.