Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Sqlite Error: cannot create INSTEAD OF trigger on table?

I try to create an instead-of trigger for an update on a table. The normal use of instead-of triggers are views but the Sqlite manual says that instead-of triggers are also valid for tables. But I get the error: cannot create INSTEAD OF trigger on table. And I am wondering why.

I use foreign keys:

PRAGMA foreign_keys = ON;

And I have two tables. An identifier table:

CREATE TABLE id
(
    id      INTEGER PRIMARY KEY AUTOINCREMENT,
    created REAL    NOT NULL DEFAULT CURRENT_TIMESTAMP
);

And a table which references the identifiers:

CREATE TABLE person
(
    id        INTEGER NOT NULL DEFAULT (last_insert_rowid()) REFERENCES id,
    login     TEXT,
    password  TEXT,
    firstname TEXT,
    lastname  TEXT,
    email     TEXT,
    created   REAL  NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id, created)
);

Insert works fine:

INSERT INTO id DEFAULT VALUES;
INSERT INTO person (login) VALUES ('root');
SELECT * FROM person;
1|root|||||2012-02-28 18:03:45

Now I want to define the following trigger, which converts an update into an insert:

CREATE TRIGGER person_update INSTEAD OF UPDATE OF login, password, firstname, lastname, email ON person
BEGIN
    INSERT INTO person (login, password, firstname, lastname, email)
    VALUES (new.login, new.password, new.firstname, new.lastname, new.email);
END;

But it fails with the above error and I do not understand why.

like image 379
ceving Avatar asked Feb 28 '12 18:02

ceving


Video Answer


2 Answers

I tried going through the source code (search for "cannot create INSTEAD OF") and despite reading your link in the comment where the create trigger statement is outlined in great detail, I would say that triggers with INSTEAD OF are only for views and cannot be used with tables.

like image 143
Till Avatar answered Oct 12 '22 11:10

Till


I'm hoping that this is just a typo in the post, but your create statement should read:

INSTEAD OF UPDATE ON

you've typed:

INSTEAD OF UPDATE OF 
like image 39
Inuoa Avatar answered Oct 12 '22 11:10

Inuoa