Showing results for 
Search instead for 
Did you mean: 

Toggle relationship

Graph Voyager

I have found a link which states to toggle the relationship:

MATCH (u:User)
WHERE id(u) = 1
WHERE id(p) = 1
CREATE (u)-[:LIKES]->(p)
WITH u, p
MATCH (u)-[r:LIKES]->(p), (u)-[:LIKES]->(p)

But I'm not able to understand how toggle is working here.

Also, when I tried to count the relationship, it returns 0 and 2 instead of 0 and 1. Why?

RETURN count(r) // returns 2 at first and 0 at second try

What do you mean by "toggling a relationship?"

Also, if there is only this single :LIKES relationship, the MATCH will fail, as the same relationship cannot be traversed twice to match the pattern.

I meant for when we hit the query for second time it would remove the relationship. And hitting the query again it will create the relationship. What's here I am not able to understand why is it returning 2 instead of 1?

I don't understand how that's supposed to work, the last MATCH won't match unless there are two :LIKES relationships between the nodes, I don't think this is doing what you think it's doing. Also, there's no way two separate nodes would have the same graph id. I think you probably put that in as a placeholder.

This would work instead.

MATCH (u:User), (p:Post)
WHERE id(u) = 123 AND id(p) = 456 // better to lookup by property instead
MERGE (u)-[r:LIKES]->(p)
ON MATCH SET r.toDelete = true
WHERE r.toDelete // which is only set to true when we matched to an existing relationship