I have a following schema in my sqlite3 database.
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.
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;
实际上这是一个特定于应用程序的错误,我可以在发布之前找到它。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.