简体   繁体   中英

SQLite trigger to delete record that activated the trigger

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.

Try using references like this or this one.

May both be able to help you in your case. You code to me seems to have some issues as well, look into that. You must create a trigger for that trigger, similarly shown in the links I've provided.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM