简体   繁体   English

使用 arangodb 中的 1:n 连接确保数据完整性(自动删除孤立顶点)

[英]Ensuring data integrity with 1:n connections in arangodb (delete orphan vertex automatically)

Say I have a users document group and another document group sessionTokens that store the session the users have.假设我有一个users文档组和另一个存储用户会话的文档组sessionTokens Each user can have multiple tokens but a token can only belong to one user.每个用户可以拥有多个令牌,但一个令牌只能属于一个用户。 The association is done through edges.关联是通过边完成的。

Now if I use graph functions I can delete a token and the edge between the user and the token gets deleted automatically, and that is great.现在,如果我使用图形函数,我可以删除一个标记,并且用户和标记之间的边会自动删除,这很棒。 But what if I want to make sure that if a user is deleted, I want all their session tokens (vertices) to be deleted automatically as well (or else they will be orphan tokens)?但是,如果我想确保如果用户被删除,我希望他们的所有会话令牌(顶点)也被自动删除(否则它们将成为孤儿令牌)怎么办? Do I have to handle this in application code?我必须在应用程序代码中处理这个吗? Or is there a declarative way to ensure this type of integrity?或者是否有一种声明方式来确保这种完整性?

According to the docs ,根据文档

Deleting vertices with associated edges is currently not handled via AQL while the graph management interface and the REST API for the graph module offer a vertex deletion functionality.删除具有关联边的顶点当前不通过 AQL 处理,图形管理接口图形模块REST API提供了顶点删除功能。

That said, there are ways to cheat the system, somewhat.也就是说,有些方法可以欺骗系统。 For instance, you can modify several collections at once.例如,您可以一次修改多个集合。 Therefore, you could collect the _key values for all the nodes and edges related to the vertex you're removing, then remove each of those in turn.因此,您可以收集与要删除的顶点相关的所有节点和边的_key值,然后依次删除每个节点和边。

FOR u IN users
    FOR v,e IN 1 INBOUND u users_sessionTokens
        COLLECT del_users = u._key, del_edges = e._key, del_sessionTokens = v._key
        LET vx = (
            FOR d IN del_sessionTokens
                REMOVE d IN sessionTokens
        )
        LET ex = (
            FOR d IN del_edges
                REMOVE d IN users_sessionTokens
        )
        LET ux = (
            FOR d IN del_users
                REMOVE d IN users
        )

It's not necessarily the best implementation, but might work well wrapped in a transaction.它不一定是最好的实现,但可以很好地封装在事务中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM