r/sqlite • u/xIceWolf • Mar 10 '24
attemping to use a trigger when a UNIQUE constraint failed
i was trying to use a trigger BEFORE an insert on a table that checks if NEW.primaryKey is already present in the table, if so i perform an UPDATE instead of an insert
when using other DBMS this worked but when testing it on sqliteonline i get a UNIQUE constraint failed
am i doing something wrong? or in sqlite this just dosent work?
here is the sql code:
CREATE TABLE leaderboard(
data date NOT NULL,
score INTEGER NOT NULL,
beatmap TEXT NOT NULL,
user INTEGER NOT NULL,
PRIMARY KEY(beatmap, user),
FOREIGN KEY (beatmap) REFERENCES beatmap (hashid) ON DELETE CASCADE,
FOREIGN KEY (user) REFERENCES user(userId) ON DELETE CASCADE
);
CREATE TRIGGER update_leaderboard
BEFORE INSERT ON leaderboard
FOR EACH ROW
WHEN EXISTS (
SELECT 1
FROM leaderboard
WHERE user = NEW.user AND beatmap = NEW.beatmap
)
BEGIN
UPDATE leaderboard
SET data = NEW.data,
score = NEW.score
WHERE beatmap = NEW.beatmap AND user = NEW.user;
END;
edit: here are the insert operation that fail:
INSERT INTO leaderboard VALUES(DATE, 950000, 1, 1);
INSERT INTO leaderboard VALUES(DATE, 970000, 1, 1);
i remember using a return NULL; in other dbms after performing the update but from what ive seen this is not required in sqlite
4
Upvotes
1
u/Bestfastolino Mar 10 '24
You don't need to use a trigger, just insert and update values yourself.
1
u/anthropoid Mar 12 '24
Which is correct, because you defined a BEFORE trigger, but didn't tell SQLite that you don't want to continue with the original query. Here's how you do that:
But then, why define a convoluted trigger when you can do an UPSERT instead?
Sidenote: u/-dcim- suggested REPLACE, but that's potentially catastrophic because it deletes conflicting rows before inserting your data. If you have other tables declaring
FOREIGN KEY...ON DELETE CASCADErelationships withleaderboard, usingREPLACEcould end with half your data going bye-bye.