Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

after insert trigger does not work when insert happens from node js app

I have a after insert trigger. that calls a stored procedure .when I run a direct query it works well. but when I insert data through node js app . it does not even insert data in main table . There is not any error in trigger and stored procedure cause it works with direct insert query for the table .

dbObject.create(data, function(err, response){
        if (err) {
            //console.log("err", err);
            res.status(500).jsonp(response)
        }
        console.log("response", response);
        dbObject.find({SnsId: SnsId}, '*' , {}, 0, 0, {}, function(err, resp){
            if (err) {
                //console.log("err", err);
                res.status(500).jsonp(err)
            }
            console.log("response", resp);
            res.jsonp(resp);
        });
    });

trigger code --

DELIMITER $$
CREATE TRIGGER `copytoSensorMapTrigger` AFTER INSERT ON test4.`SENSORS`
FOR EACH ROW
BEGIN
   CALL copyToSensorMap(NEW.`SnsId`,NEW.`SnsState`);
END;

Stored Procedure code --

DELIMITER $$
 CREATE PROCEDURE copyToSensorMap(IN snsid bigint(20),IN sns_state varchar(128))
   BEGIN
   DECLARE  color varchar(128);
   INSERT INTO test3.`SENSOR_MAP` (`SENSOR_ID`, `SENSOR_NAME`, `USER_ID`, `HOST_ID`,  `SENSOR_STATE`)
    SELECT `SnsId`,
   `SnsName`,`SnsOwner`,`HostId`,`SnsState`
         FROM test4.`SENSORS`
         where test4.`SENSORS`.`SnsId`=snsid;
    IF ( sns_state ='Unassigned') THEN
        set color = 'Grey';
    ELSEIF ( sns_state ='Running') THEN
     set color = 'Green';
    END IF;
    update test3.`SENSOR_MAP`
       set test3.`SENSOR_MAP`.`SENSOR_COLOR` = color
       where test3.`SENSOR_MAP`.`SENSOR_ID` = snsid;
UPDATE test3.`SENSOR_MAP`
  INNER JOIN test4.`HOSTS`
    ON (test3.`SENSOR_MAP`.`HOST_ID` = test4.`HOSTS`.id)
  SET 
    test3.`SENSOR_MAP`.HOST_NAME = test4.HOSTS.Name,
    test3.`SENSOR_MAP`.LATITUDE = test4.HOSTS.GpsLatitude,
    test3.`SENSOR_MAP`.LONGITUDE = test4.HOSTS.GpsLongitude;
UPDATE test3.`SENSOR_MAP`
  INNER JOIN test4.`users` ON (test3.`SENSOR_MAP`.`USER_ID` = test4.`users`.id)
  SET 
    test3.`SENSOR_MAP`.USER_NAME = test4.users.user;
UPDATE  test3.`SENSOR_MAP`  c
    INNER JOIN
    (
        select a.`SnsId`,b.id,b.ServiceName,b.ServiceType
           from test4.sensors as a
           join test4.SESSIONS as b  on a.SessionId =b.id
           where a.`SnsId` =snsid
           group by `SnsId` 
    ) d  ON c.SENSOR_ID = d.`SnsId`
    SET c.SESSION_NAME = d.ServiceName,
        c.SESSION_TYPE = d.ServiceType;
END $$

I am using Mysql database and storage engine is InnoDB. I have checked the sql query . it is forming with SET Keyword. does Mysql insert query with set keyword support after insert stored procedure.

Issue is solved now. it was a table name in all caps in Mysql and I had written in lowercase.tables names were "user" and "sensor" . now it is solved.

like image 223
Mohit Sahu Avatar asked Jan 14 '16 13:01

Mohit Sahu


People also ask

What if after insert trigger fails?

If the trigger errors, the trigger work and insert will be rolled back. Triggers execute with SET XACT_ABORT ON by default, which will rollback the transaction after an error. So if you attempt a transaction. Commit() after the error, that will fail because no transaction is active.

What is after insert trigger?

An AFTER INSERT Trigger means that MySQL will fire this trigger after the INSERT operation is executed.

What does before insert trigger do?

BEFORE triggers cannot change tables, but they can be used to verify input column values and to change column values that are inserted or updated in a table. In the following example, the trigger is used to set the fiscal quarter for the corporation before inserting the row into the target table.


1 Answers

I would also recommend to use return in the places you check for errors, otherwise the code will continue and not stop in case of errors.

Try using this code:

dbObject.create(data, function(err, response){
    if (err) {
        //console.log("err", err);
        return res.status(500).jsonp(response)
    }
    console.log("response", response);
    dbObject.find({SnsId: SnsId}, '*' , {}, 0, 0, {}, function(err, resp){
        if (err) {
            //console.log("err", err);
            return res.status(500).jsonp(err)
        }
        console.log("response", resp);
        res.jsonp(resp);
    });
});
like image 171
Gonen Avatar answered Sep 26 '22 14:09

Gonen