简体   繁体   English

Py2neo V3-来自相同节点的多重关系

[英]Py2neo V3 - Multiple Relationship from the same nodes

I'm trying to create multiple relationship from the same nodes, in my case, my user needs to be able to comment more than one time in my Post. 我试图从同一节点创建多个关系,以我为例,我的用户需要能够在我的帖子中发表多次评论。 I did that by creating an relationship between User and Post. 我通过在User和Post之间创建关系来做到这一点。 But when I try to create it, it actually updates the old relationship. 但是当我尝试创建它时,它实际上更新了旧的关系。 Have I done anything wrong? 我做错了什么吗? Is there an better way to do this? 有更好的方法吗?

graph.schema.create_uniqueness_constraint('COMMENTS', 'uuid')

def comment(self, post_uuid, comment):
    post = self.graph.find_one('Post','uuid', post_uuid)
    user = self.graph.find_one('User','uuid', self.uuid)
    r_comment = Relationship(user, "COMMENTS", post, comment=comment, uuid=uuid4().hex, date=str(datetime.utcnow()))
    self.graph.create(r_comment)
    return True

This type of model is not supported by the higher level py2neo API. 更高级别的py2neo API不支持这种类型的模型。 You'll have to drop down into Cypher to work with this. 您必须进入Cypher才能使用此功能。

Consider though whether your model is extensible in its current form. 考虑一下您的模型是否可以以当前形式扩展。 The reason for this design decision in py2neo is that this kind of model is often non-optimal and could generally be improved by adding another node. 在py2neo中做出此设计决定的原因是,这种模型通常不是最佳模型,通常可以通过添加另一个节点来进行改进。 In your case a node would represent a comment. 在您的情况下,节点将代表注释。

So instead of having: 因此,您不必:

(:User)-[:COMMENTS_ON]->(:Post)

You would have: 你将会拥有:

(:User)-[:WRITES_COMMENT]->(:Comment)-[:RELATES_TO_POST]->(:Post)

This extracts another "noun" in your model into a new node type. 这会将模型中的另一个“名词”提取到新的节点类型中。 Consequently you can now make links to the comment itself, which is not possible if you model it as a relationship. 因此,您现在可以建立到评论本身的链接,如果将其建模为关系则是不可能的。

Hope this helps. 希望这可以帮助。

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

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