考慮以下模式:授權邀請用戶通過數據庫觸發器
Table "public.users"
Column | Type | Modifiers
----------------------+--------------------------+------------------------------------------------------------
uuid | uuid | not null default uuid_generate_v4()
email | character varying(254) | not null
name | text | not null
created_at | timestamp with time zone | not null default now()
Table "public.users_projects"
Column | Type | Modifiers
-----------------+--------------------------+--------------------------------------------
project_uuid | uuid | not null
user_uuid | uuid | not null
invitation_uuid | uuid |
membership_type | membership_type | not null default 'member'::membership_type
created_at | timestamp with time zone | not null default now()
Table "public.projects"
Column | Type | Modifiers
------------+--------------------------+-------------------------------------
uuid | uuid | not null default uuid_generate_v4()
name | character varying(100) | not null
created_at | timestamp with time zone | not null default now()
Table "public.invitations"
Column | Type | Modifiers
-----------------+--------------------------+--------------------------------------------
uuid | uuid | not null default uuid_generate_v4()
email | character varying(254) | not null
target_type | character varying(50) | not null
target_uuid | uuid | not null
membership_type | membership_type | not null default 'member'::membership_type
token | character varying(32) | not null default md5((random())::text)
creator_uuid | uuid | not null
也有一個枚舉類型CREATE TYPE membership_type AS ENUM ('guest', 'member', 'manager', 'owner');
。
我想知道如果我可以使用觸發器來驗證用戶不能創建一個資源的邀請,至少沒有連接表上的成員類型manager
。
我的主要問題是:
- 我可以將該行插入「之前」使用觸發器,
CREATE TRIGGER check_authorisation BEFORE INSERT OR UPDATE ON invitations FOR EACH ROW EXECUTE PROCEDURE check_authorisation();
- 我多少信息可以給回一條錯誤消息,因爲對剛剛失敗,因爲數據庫連接斷開等
- 這是一個理智的方法嗎?作爲一個Web應用程序開發人員,我習慣於在應用程序代碼中檢查這個環境,但是我正在處理將由兩個應用程序共享的數據庫(Golang和Ruby)
- 我可以實現
REFERENCES
在多態字段(target_type
,target_uuid
)以某種方式?