[英]SQL trigger: BEFORE UPDATE OF
I have a following schema in my sqlite3 database. 我的sqlite3数据库中有以下模式。
It is a table that stores user's bid on a product, and triggers that I use to offload a validity check from my application code. 它是一个存储用户对产品的出价的表格,以及用于从我的应用程序代码中卸载有效性检查的触发器。
The goal here is to check whether a new bid is larger than the previous one and if not then raise an error to the application. 这里的目标是检查新的出价是否大于前一个出价,如果没有,则向应用程序提出错误。 It works well for
INSERT
, but UPDATE
raises error even when NEW.amount
is actually the largest bid for a given product. 它适用于
INSERT
,但即使NEW.amount
实际上是给定产品的最大出价, UPDATE
也会引发错误。
I cannot figure out why is this happening mainly because triggers are impossible to debug. 我无法弄清楚为什么会发生这种情况主要是因为触发器无法调试。 A suggestion on this would also be greatly appreciated.
对此的建议也将不胜感激。
CREATE TABLE IF NOT EXISTS bids (
amount INTEGER NOT NULL,
product_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
submitted DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TRIGGER IF NOT EXISTS trig_beforeinsert BEFORE INSERT ON bids
BEGIN
SELECT CASE
WHEN
((SELECT MAX(amount) FROM bids WHERE product_id = NEW.product_id) >= NEW.amount)
THEN
RAISE (FAIL, "invalid amount")
END;
END;
CREATE TRIGGER IF NOT EXISTS trig_beforeupdate_amount BEFORE UPDATE OF amount ON bids
BEGIN
SELECT CASE
WHEN
((SELECT MAX(amount) FROM bids WHERE product_id = OLD.product_id) >= NEW.amount)
THEN
RAISE (FAIL, "invalid amount")
END;
END;
实际上这是一个特定于应用程序的错误,我可以在发布之前找到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.