Felipe Santiago
Felipe Santiago

Reputation: 444

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. 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

Upvotes: 1

Views: 627

Answers (1)

Nigel Small
Nigel Small

Reputation: 4495

This type of model is not supported by the higher level py2neo API. You'll have to drop down into Cypher to work with this.

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. 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.

Upvotes: 2

Related Questions