gpt4 book ai didi

function - 通过数据库触发器邀请用户的授权

转载 作者:行者123 更新时间:2023-11-29 14:00:47 24 4
gpt4 key购买 nike

给定以下架构:

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 成员类型。

我的主要问题是:

  • 我可以在插入行“之前”使用触发器吗?
  • 我可以在错误消息中返回多少信息,而不是仅仅因为数据库连接断开等而失败
  • 这是明智的做法吗?作为 Web 应用程序开发人员,我习惯了在应用程序代码中进行检查的环境,但我正在处理将由两个应用程序(Golang 和 Ruby)共享的数据库
  • 我能否以某种方式在多态字段(target_typetarget_uuid)上实现REFERENCES

最佳答案

Can I use a trigger "before" the row is inserted, CREATE TRIGGER check_authorisation BEFORE INSERT OR UPDATE ON invitations FOR EACH ROW EXECUTE PROCEDURE check_authorisation();

使其成为(后)约束触发器,并在适当的时候引发异常。

更一般地说,使用前触发器在需要时修改传入数据,使用后触发器传播副作用,使用约束触发器验证完整性。

How much information can I give back in an error message, as against just failing because the database connection is down, etc

在引发异常时发送多少就发送多少。

Is this a sane approach? As a web application developer, I'm used to an environment where this would be checked in application code, but I'm working on a database that will be shared by two applications (Golang and Ruby)

如果处理得当,它可能是理智的,但恕我直言,正确管理授权时出现的所有奇怪情况基本上是不可能的,因此这种类型的逻辑通常最好放在数据库之外。

举个例子:当经理的秘书的替代者(原来那个正在休产假)这样做时会发生什么?

Can I implement a REFERENCES on the polymorphic fields (target_type, target_uuid) somehow?

您可以使用后触发器。但是根据您正在做的事情,考虑使用单独的字段、外键和检查约束对其进行规范化,以确保它们相互排斥。与最终手动重写外键逻辑相比,管理起来通常更容易/更清晰。

关于function - 通过数据库触发器邀请用户的授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19389972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com