I want to create a trigger in order to delete a record 2 minutes after it has been created. This will be used in a 'recover password' scenario so those 2 minutes will be the time window for someone to use the validation code to recover their password. SQLite is what I am working on. Here's the situation:
CREATE TABLE IF NOT EXISTS password_recovery(
email VARCHAR(50) NOT NULL UNIQUE,
confirmation_id INTEGER(5) PRIMARY KEY,
FOREIGN KEY (email) REFERENCES users (email)
ON DELETE NO ACTION ON UPDATE NO ACTION,
);
CREATE TRIGGER IF NOT EXISTS password_recovery_deletion_timer AFTER INSERT ON password_recovery
BEGIN
DO SLEEP(120);
// i want to delete here but how do I pass which record triggered the trigger?
END;
You could use a (primary) key of the table:
DELETE FROM password_recovery WHERE rowid = NEW.rowid;
However, triggers are executed as part of the statement that triggered them. So the original INSERT statement would wait for two minutes, and the login would not be able to read this row from the database because the insert transaction would not have finished yet.
The only correct way to implement this is to make the recovery request and the recovery two separate transactions.
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.