Sqlite - Too Many levels of Trigger recursion

Asked

Viewed 173 times

2

I’m getting the error below when I try to make an insertion in Sqlite.

Error while executing SQL query on database 'BDTESTE': Too Many levels of Trigger recursion

Table creation

CREATE TABLE INSTANCES (
ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
NAME varchar(255) NOT NULL UNIQUE,
DESCRIPTION text,
CONFIG_TXT text,
EXTRA_TXT text,
CREATED varchar(50) NULL,
MODIFIED varchar(50) NULL
);

Creation of Trigger

CREATE TRIGGER INSTANCE_INSERT AFTER INSERT ON INSTANCES
BEGIN
UPDATE INSTANCES SET CREATED = DATETIME('NOW') WHERE ID = NEW.ID;
END;

Creation of Trigger - Update

CREATE TRIGGER INSTANCE_UPDATE AFTER UPDATE ON INSTANCES
BEGIN
UPDATE INSTANCES SET MODIFIED = DATETIME('NOW') WHERE ID = OLD.ID;
END;

Insertion

INSERT INTO INSTANCES (NAME, DESCRIPTION, CONFIG_TXT, EXTRA_TXT)
VALUES
('app02', 'Intancia de teste Windows', 'blah blah blah blah blah blah blah 
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah', '');

1 answer

4


The problem is in trigger of update. The update will generate the firing of trigger, then you will be doomed to an infinite loop if you do not treat correctly.

Maybe the following works (ignores updates that don’t set new information):

CREATE TRIGGER INSTANCE_UPDATE AFTER UPDATE ON INSTANCES
BEGIN
    UPDATE INSTANCES SET MODIFIED = DATETIME('NOW')
    WHERE ID = OLD.ID AND MODIFIED != DATETIME('NOW');
END;

This will prevent you from vacuously updating any line.

Another alternative would be to operate on views with triggers, then I put it here. It requires quite a bit more code.

  • I changed as your example and the insertion worked, but I performed an update and did not update the field "MODIFIED"

  • @Lucassouza, Oops? Need to test more then...

  • 1

    I ran this update "UPDATE INSTANCES SET NAME = 'app01' WHERE ID = 2", updated the data but the MODIFIED field did not.. Then if you can test and tell me, I’m grateful...

  • @Lucassouza I speak yes, I hope I can test today still

  • Quiet! I’m waiting!

  • It worked the update too, only on some tables that doesn’t work.. I think it’s for the IDE, because yesterday it wasn’t working on a table and today it worked.... Thank you!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.