참고로, 저는 이 문제를 제가 운영하는 포럼에서 데이터베이스 레벨에서 "해결"했습니다. 다른 더 고수준의 솔루션을 사용할 수 없는 경우에만 이 방법을 사용하세요. (이 문제에 대해 어떤 진전이 있었나요, 아니면 기존 답변 중 하나를 "해결됨"으로 표시해야 하나요?)
다음은 PostgreSQL 코드입니다:
create or replace function anon_insert_copy_groups_to_anon_user()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS $$
declare
anon_user_id integer = new.user_id;
master_user_id integer = new.master_user_id;
active boolean = new.active;
created_at timestamp = new.created_at;
updated_at timestamp = new.updated_at;
BEGIN
insert into group_users
(group_id, user_id, created_at, updated_at, owner, notification_level, first_unread_pm_at)
select group_users.group_id, anon_user_id , current_timestamp, current_timestamp, false, 3 as notification_level, current_timestamp
from group_users
where group_users.user_id=master_user_id
and group_users.group_id not in (select group_id from group_users where group_users.user_id=anon_user_id)
and group_users.group_id not in (1, 2, 3); -- 관리자, 모더레이터, 스태프 제외!
return new;
end;
$$;
-- 트리거 생성
CREATE TRIGGER anon_insert_trigger
AFTER INSERT ON anonymous_users
FOR EACH ROW
EXECUTE FUNCTION anon_insert_copy_groups_to_anon_user();