Create relationship after UNWIND

After I run my code here:

WHERE id(a) < 80 and id(c) < 80
WITH COLLECT([b,d]) as calls
UNWIND calls as callss
FOREACH (i IN callss | CREATE (d)-[r:KNOWS]->(b))
RETURN callss

I have this list:

Now for each one in the list, I want to CREATE relationship "KNOWS" between two nodes. But I don't know how to do that. Please give me answer.

Thank you very much for your time.

Hello @iamtienng and welcome to the Neo4j community :slight_smile:

WHERE id(a) < 80 AND id(c) < 80
CREATE (d)-[r:KNOWS]->(b)

Why are you doing id(a) < 80 and not id(a) = 80 ?


Thank you very much for your reply.

I did write id(a) < 80 instead of id(a) = 80 because I have around 1000 cases to do.
I used your solution and it still causes the error:

In my desire, I just want each b and d to create a relationship as if I used 'id (a) = 80)'

Best regards,
Tien Nguyen.

I found the solution:

UNWIND range(0, 100) as idf
WHERE id(a)=idf and id(c)=idf
CREATE (d)-[r:KNOWS]->(b)
RETURN a,b,c,d

Thank you very much.

Best regards,
Tien Nguyen.

1 Like

Usage of graph ids like this isn't recommended. If a node is deleted, its id becomes eligible for reuse in the future, so an approach like this doesn't have many guarantees about the nodes it will operate over.

What is weird here is that your query seems more complex than it needs to be, and it obscures what you're trying to do.

Your latest query shows that the calls a and c are actually the same node, since your restriction is that they have the same graph id. This is the same call! If all you're trying to do is MATCH to the caller and receiver of the same call and create a :KNOWS relationship between them, then we can do this far more clearly we just use a single CALL node:

MERGE (d)-[:KNOWS]->(b)
RETURN d, c, b

Dear Andrew,

Your solution is so simple but yet extremely effective. Maybe I have not fully understood how Neo4j works.
Thank you so much for the replies even though I haven't logged in in a long time.

Best regards,
Tien Nguyen.