我有一个相邻列表表帐户,带有列 account_id
, name
,和 parent_account_id
. 为了便于排序和显示,我创建了一个包含以下列的闭包表: ancestor
, descendant
,和 depth
. 因此,为了自动保持深度,我为account表创建了一个触发器和一个触发器函数:
create or replace function insert_account_relations_after_insert_account()
returns trigger as
$$
declare
_ancestor account_relations%rowtype;
begin
insert into account_relations (ancestor, descendant, depth, created_by, updated_by)
values (NEW.account_id, NEW.account_id, 0, NEW.created_by, NEW.updated_by);
if (NEW.parent_account_id is not null) then
for _ancestor in select ar.ancestor, ar.depth from account_relations ar where ar.descendant = NEW.parent_account_id
loop
insert into account_relations (ancestor, descendant, depth, created_by, updated_by)
values (_ancestor.ancestor, NEW.account_id, _ancestor.depth + 1, NEW.created_by, NEW.updated_by);
end loop;
end if;
return NEW;
end;
$$
language plpgsql;
drop trigger if exists trigger_insert_account_relations_after_insert_account on accounts;
create trigger trigger_insert_account_relations_after_insert_account
after insert
on accounts
execute procedure insert_account_relations_after_insert_account();
但是,当我插入一个全新的帐户(无论 parent_account_id
是否设置),hibernate抛出 DataIntegrityViolationException
具体如下:
Resolved DataIntegrityViolationException with error ERROR: null value in column "ancestor" of relation "account_relations" violates not-null constraint
Detail: Failing row contains (159489f1-3a05-411b-a723-efb3c769af95, null, null, 0, 2021-04-19 02:41:15.907311+07, 2021-04-19 02:41:15.907311+07, null, null).
Where: SQL statement "insert into account_relations (ancestor, descendant, depth, created_by, updated_by)
values (NEW.account_id, NEW.account_id, 0, NEW.created_by, NEW.updated_by)"
我想知道这里的根本原因是什么,如何明确解决?任何人都可以帮忙。
非常感谢,
暂无答案!
目前还没有任何答案,快来回答吧!