postgres触发器无法正常工作 - id参数未正确传递给新表

时间:2018-05-23 21:25:48

标签: postgresql triggers

用于邮戳中的以下触发器:

trigger vehicle_history
before update
 on public.vehicle
 for each row 
EXECUTE PROCEDURE public.log_vehicle_history()

具有以下功能:

function log_vehicle_history()
    returns trigger as 
$BODY$ BEGIN
IF NEW.lastupdated <> OLD.lastupdated THEN
INSERT INTO vehiclehistory(vehicle_id, event, datetime, latitude, longtitude, speed) 
VALUES(OLD.id, OLD.lastevent, OLD.lastupdated, OLD.latitude, OLD.longtitude, OLD.speed);
END IF;
RETURN NEW;
END;
$BODY$ 
language PLPGSQL volatile
cost 100;
alter function log_vehicle_history() owner to postgres;

我收到以下错误:

2018-05-23 23:06:08.435 ERROR 12492 --- [       Thread-7] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: null value in column "id" violates not-null constraint
  Details: Failing row contains **(null, 2018-05-23 13:07:53, 0, 34.221820000000001, 108.88527999999999, null, 8)**.
  Where: SQL statement "INSERT INTO vehiclehistory(vehicle_id, event, datetime, latitude, longtitude, speed) 
VALUES(OLD.id, OLD.lastevent, OLD.lastupdated, OLD.latitude, OLD.longtitude, OLD.speed)"
PL/pgSQL function log_vehicle_history() line 3 at SQL statement

由于某些原因,在所有值中只缺少id - (null,2018-05-23 13:07:53,0,32.221820000000001,108.88527999999999,null,8)。在postgres中是关键词的id?如何解决?

表格是:

select
    v.datetime,
    v."event",
    v."id",
    v.latitude,
    v.longtitude,
    v.speed,
    v.vehicle_id
from
    public.vehiclehistory v;

select
    v.gpsstatus,
    v."id",
    v.imei,
    v.ip,
    v.lastevent,
    v.lastupdated,
    v.latitude,
    v.licenceplate,
    v.longtitude,
    v."name",
    v.port,
    v.speed,
    v.user_id
from
    vehicle v;

谢谢!

0 个答案:

没有答案