Reputation: 23814
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.
Upvotes: 9
Views: 6415
Reputation: 2760
Just create a view person_view as select * from person
and point your trigger to it.
Upvotes: 0
Reputation: 22408
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.
Upvotes: 14
Reputation: 19
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
Upvotes: 0