Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL Trigger works if I use 'after update' but not 'after insert'?

I've written a trigger to insert a row into table b after an operation on table a. For some reason, it has no effect if I add this trigger 'after insert' and then insert a row. However, it does work if I add the trigger as 'after update', and update the row.

Here's the trigger code. When I replace 'AFTER UPDATE' with 'AFTER INSERT', and do an insert, then nothing happens when i insert a new row. I get no errors when creating the trigger, and I am not trying to update the same table the trigger is being set upon. Any help is appreciated! thanks, Jen

drop trigger if exists insertUndecided;  
DELIMITER //  
CREATE TRIGGER insertUndecided  
     AFTER UPDATE ON jiraissue
     FOR EACH ROW
     BEGIN
        insert into nodeassociation (SOURCE_NODE_ID, SOURCE_NODE_ENTITY, SINK_NODE_ID, SINK_NODE_ENTITY, ASSOCIATION_TYPE, SEQUENCE)
   select
      NEW.id as SOURCE_NODE_ID,
      'Issue' as SOURCE_NODE_ENTITY,
      (select pv.id from projectversion pv
         where pv.vname='undecided'
         and pv.project=NEW.project ) as SINK_NODE_ID,
      'Version' as SINK_NODE_ENTITY,
      'IssueFixVersion' as ASSOCIATION_TYPE,
      NULL as SEQUENCE

   from dual  where exists
      (select pkey from jiraissue
         where id=NEW.id and id not in
            (select distinct source_node_id from nodeassociation
               where source_node_entity='Issue' and SINK_NODE_ENTITY='Version'
               and ASSOCIATION_TYPE='IssueFixVersion') );

 END;//

DELIMITER ;
like image 805
jen Avatar asked Nov 14 '22 10:11

jen


1 Answers

On MySQL 5.5.20 with InnoDB tables i've reproduced your situation in a simplified test. both, inserting using a result from the trigger table which is referring to the new row alreay and directly inserting using the NEW values worked fine

CREATE TABLE test1(a1 INT NOT NULL auto_increment, b1 INT, PRIMARY KEY (a1) );
CREATE TABLE test2(a1 INT, b1 INT);
CREATE TABLE test3(a1 INT, b1 INT);

DELIMITER ;;

CREATE TRIGGER testAI AFTER INSERT ON test1
  FOR EACH ROW BEGIN
    INSERT INTO test2 SET a1 = NEW.a1, b1 = NEW.b1;
    INSERT INTO test3 ( a1, b1 ) SELECT a1, b1 FROM test1 WHERE a1 = NEW.a1;
  END;
;;

DELIMITER ;

Fire the trigger

INSERT INTO test1 (b1) VALUES (1),(2),(3);

Directly inserted from NEW values

mysql> SELECT * FROM test2;
+------+------+
| a1   | b1   |
+------+------+
|    1 |    1 |
|    2 |    2 |
|    3 |    3 |
+------+------+
3 rows in set (0.00 sec)

Inserted from SELECT result

mysql> SELECT * FROM test3;
+------+------+
| a1   | b1   |
+------+------+
|    1 |    1 |
|    2 |    2 |
|    3 |    3 |
+------+------+
3 rows in set (0.00 sec)

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.5.20    |
+-----------+
1 row in set (0.00 sec)

So this works for both auto_increment and explicitly inserted values. Your problem must be somewhere else

like image 156
Michel Feldheim Avatar answered Dec 10 '22 07:12

Michel Feldheim