试图自动为postgresql表添加时间戳,错误:记录"新"尚未分配

时间:2015-03-17 21:40:45

标签: postgresql triggers

我想在postgre v.9.3数据表中自动为任何插入或更新的行添加时间戳。我尝试按照这里的代码:Postgresql Current timestamp on Update写信,但它对我不起作用:

db=> CREATE OR REPLACE FUNCTION upd_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.lastmodified = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
CREATE FUNCTION

db=> CREATE TRIGGER dealscheduleT
AFTER UPDATE or INSERT ON dealschedule FOR EACH STATEMENT EXECUTE PROCEDURE upd_timestamp();
CREATE TRIGGER

db=> insert into dealschedule (deals_id) values (55);
ERROR:  record "new" is not assigned yet
DETAIL:  The tuple structure of a not-yet-assigned record is indeterminate.
CONTEXT:  PL/pgSQL function upd_timestamp() line 3 at assignment

以下是我目前在表格中的内容:

 deals_id |     starttimes      | interested |      endtimes       | lastmodified
----------+---------------------+------------+---------------------+--------------
      227 | {08:00:00,13:12:00} | {426,427}  | {13:00:00,16:00:00} |
(1 row)

以下是该表的完整描述:

              Table "public.dealschedule"
    Column    |            Type             | Modifiers
--------------+-----------------------------+-----------
 deals_id     | integer                     |
 starttimes   | time without time zone[]    |
 interested   | integer[]                   |
 endtimes     | time without time zone[]    |
 lastmodified | timestamp without time zone |
Indexes:
    "dealschedule_deals_id_key" UNIQUE CONSTRAINT, btree (deals_id)
Triggers:
    dealschedulet AFTER INSERT OR UPDATE ON dealschedule FOR EACH STATEMENT EXECUTE PROCEDURE upd_timestamp()

我做错了什么?错误是什么意思?

0 个答案:

没有答案