我有父子表,我希望自动存储父记录中子项数的计数。更新不是很频繁,但是读取是,所以这是一个对我来说很容易缓存的数字。表格可能如下所示:
create table parent(
id SERIAL PRIMARY KEY,
numchildren integer not null default 0
);
create table child(
id serial primary key
parent_id integer NOT NULL REFERENCES parent(id)
);
我添加了一个更新numchildren
的触发器,但它总是将numchildren
设置为child
表中的完整记录总数,而不仅仅是特定父项的计数
CREATE OR REPLACE FUNCTION run_after_change() RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'DELETE' THEN
UPDATE parent
SET numchildren = (SELECT COUNT(*) FROM child WHERE OLD.parent_id = parent.id)
WHERE OLD.parent_id = parent.id;
RETURN OLD;
ELSIF TG_OP = 'UPDATE' OR TG_OP = 'INSERT' THEN
UPDATE parent
SET numchildren = (SELECT COUNT(*) FROM child WHERE NEW.parent_id = parent.id)
WHERE NEW.parent_id = parent.id;
RETURN NEW;
END IF;
END; $$ language 'plpgsql';
CREATE TRIGGER after_change
AFTER DELETE OR INSERT OR UPDATE ON child
FOR EACH ROW EXECUTE PROCEDURE run_after_change();
我做错了什么?
答案 0 :(得分:2)
count(*)
查询的where子句是错误的。
parent.id
应该是parent_id
应该是:
SELECT COUNT(*) FROM child WHERE parent_id = NEW.parent_id
(或OLD.parent_id
部分DELETE
)