I'm trying to insert duplicates data to table B when PKA violated using a BEFORE INSERT TRIGGER as the following example:
CREATE TABLE A( Col INTEGER, Coll TEXT(25), CONSTRAINT PKA PRIMARY KEY(Col, Coll) ON CONFLICT IGNORE);
CREATE UNIQUE INDEX IX_A ON A(Col, Coll);
CREATE TABLE B( Col INTEGER, Coll TEXT(25));
CREATE INDEX IX_B ON B(Col, Coll);
CREATE TRIGGER Trig
BEFORE INSERT
ON A
WHEN (Col = New.Col AND Coll = New.Coll)
BEGIN
INSERT INTO B(Col, Coll) VALUES(New.Col, New.Coll);
END;
But, it seems like the column Col is not accessible there, so it throws:
no such column: Col
Even when I change the conditions to
New.Col IN(SELECT Col FROM A)
AND
New.Coll IN(SELECT Coll FROM A)
I get another error message:
UNIQUE constraint failed: A.Col, A.Coll
While it shouldn't because of ON CONFLICT IGNORE.
- Why did I get those error messages? (What's the cause).
- How can I use the trigger to insert duplicates into another table?
You don't need the index:
because you have already defined
(Col, Coll)as thePRIMARY KEYand furthermore, with this index, although you haveON CONFLICT IGNOREdefined for a duplicate row, you will receive an error if you try to insert a duplicate row, becauseON CONFLICT IGNOREis not defined for the index.So drop it:
Now, change the code of the trigger to this:
EXISTSchecks the tableAif it already contains a row with column values the same as the ones to be inserted and if it does then the new row is inserted in tableB.You could also write the trigger like this: