Search code examples
scalatriggershsqldb

HSQL Triggers : user lacks privilege or object not found: NEWROW.ID


I am trying to implement triggers in hsql after update where I have one table called component table and on update in that table I want to log it in another table using after insert trigger, for which I am doing

CREATE TABLE IF NOT EXISTS "component"(
   "id" INTEGER IDENTITY,
   "name" VARCHAR(100),
   "configuration" LONGVARCHAR,
   "owner_id" INTEGER   );

CREATE TABLE IF NOT EXISTS "component_audit"(
   "id" INTEGER IDENTITY,
   "component_id" INTEGER ,
   "action" VARCHAR(20),
   "activity_time" BIGINT,
   "user_id" INTEGER,
   FOREIGN KEY ("component_id") REFERENCES "component"("id") ON UPDATE RESTRICT ON DELETE CASCADE
);

    CREATE TRIGGER trig AFTER INSERT ON "component"
       REFERENCING NEW ROW AS newrow
       FOR EACH ROW
       INSERT INTO "component_audit" ("id","component_id","action","activity_time","user_id")
       VALUES (DEFAULT, 1, newrow.id, 123, 1);

On running HSQL throws error

Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: NEWROW.ID

Its due to my id column being in "id" because I needed it in small caps (by DEFAULT HSQLDB is upper case) how do I pass my variable subsitution ?


Solution

  • Just use the same naming as in your CREATE TABLE statement.

    CREATE TRIGGER trig AFTER INSERT ON "component"
       REFERENCING NEW ROW AS newrow
       FOR EACH ROW
       INSERT INTO "component_audit" ("id","component_id","action","activity_time","user_id")
       VALUES (DEFAULT, 1, newrow."id", 123, 1);