我正在使用PostgreSQL 9.6.1。任何人都可以解释为什么下面的更新声明没有效果?
db=> SELECT name, contact_type_id, is_default, mod_ts FROM master.contact_type
WHERE contact_type_id = 'c255e0f7-16b1-4f12-9ae4-8fca66073ad3';
name | contact_type_id | is_default | mod_ts
---------+--------------------------------------+------------+----------------------------
Bar Foo | c255e0f7-16b1-4f12-9ae4-8fca66073ad3 | f | 2017-09-13 14:40:00.961006
(1 row)
db=> UPDATE master.contact_type
SET name = 'Foobar Foo'
WHERE contact_type_id = 'c255e0f7-16b1-4f12-9ae4-8fca66073ad3';
UPDATE 1
db=> SELECT name, contact_type_id, is_default, mod_ts FROM master.contact_type
WHERE contact_type_id = 'c255e0f7-16b1-4f12-9ae4-8fca66073ad3';
name | contact_type_id | is_default | mod_ts
---------+--------------------------------------+------------+----------------------------
Bar Foo | c255e0f7-16b1-4f12-9ae4-8fca66073ad3 | f | 2017-09-13 14:45:15.257798
(1 row)
如上所示,name
字段的值不会更改。我也看到没有提出任何错误。
以下是contact_type表的说明:
db=> \d master.contact_type
Table "master.contact_type"
Column | Type | Modifiers
-----------------+-----------------------------+------------------------
contact_type_id | uuid | not null
gid_number | uuid | not null
name | character varying(75) | not null
mod_ts | timestamp without time zone | not null default now()
is_default | boolean | not null default false
Indexes:
"pk_contact_type" PRIMARY KEY, btree (contact_type_id) WITH (fillfactor='100')
"unq_contact_type__gid_number__name" UNIQUE CONSTRAINT, btree (gid_number, name) WITH (fillfactor='90')
"idx_contact_type__gid_number" btree (gid_number) WITH (fillfactor='70')
"idx_contact_type__name" btree (gid_number, lower(name::text)) WITH (fillfactor='70')
Referenced by:
TABLE "master.contact" CONSTRAINT "fk_contact__contact_type" FOREIGN KEY (contact_type_id) REFERENCES master.contact_type(contact_type_id) ON DELETE RESTRICT
Triggers:
trg_contact_type_delete_update__preserve_default BEFORE DELETE OR UPDATE ON master.contact_type FOR EACH ROW EXECUTE PROCEDURE master.check_contact_type__is_default()
trg_master_contact_type__mod_ts BEFORE INSERT OR UPDATE ON master.contact_type FOR EACH ROW EXECUTE PROCEDURE common.update_stamp()
以下是触发函数定义:
CREATE FUNCTION master.check_contact_type__is_default() RETURNS TRIGGER AS $check_contact_type__is_default$
BEGIN
IF OLD.is_default THEN
RAISE EXCEPTION 'Cannot update or delete default Contact Types shipped with the product.';
END IF;
RETURN OLD;
END;
$check_contact_type__is_default$ LANGUAGE plpgsql;
CREATE FUNCTION common.update_stamp() RETURNS TRIGGER AS $update_stamp$
BEGIN
NEW.mod_ts = current_timestamp;
RETURN NEW;
END;
$update_stamp$ LANGUAGE plpgsql;
答案 0 :(得分:2)
触发器master.check_contact_type__is_default()
返回OLD
值,因此更新前的值。您想要返回NEW
值。