Multiple relationships with one query, loop-function?!?

Hello, I have an issue and I hope you can help me.
I have imported some csv-Files to create nodes and it worked. They include just generic simple data.
For example:

Now I want to create multiple relationships with just one query. Of course I can do it like that ...

but I want something like an loop, that works incremently depend on the ID`s.
For example (pid=1) on (aid=10) (pid=2) on (aid=11) and so on.
Is there an opportunity to do that just with cypher-code or any tools ?

depending on how large of a loop but one could

unwind [1,2,3,4,5,6,7,8,9,10] as loop
match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})  
create (p1)-[:LIVING_IN]->(a1);

though in the above the first pass would evaluate to {pid: 1} and {aid: 11} , the 2nd would be {pid: 2} and {aid: 12} and isnt exactly what you want so maybe {aid: 10+loop} should be {aid:9+loop} so as to get {pid: 1} and {aid: 10} and the 2nd pass of {pid: 2} and {aid: 11} etc. Simply an example

Interesting, but I have round about 200 pid`s that have to be related. This could work for my other nodes with less datasets.

ah try

foreach ( loop in range (1,200) | match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})  create (p1)-[:LIVING_IN]->(a1);

Your first recommendation unfortunately does not create anything:

The loop-Version does not work, because of the syntax I guess.

So I tried that with a match before the foreach, but now it wants to create an existing node. I just want the combining by a new relation :(

regarding the syntax error my mistake. you need one final trailing ')' at the end of the line and thus as well as my mistake again in that you can not do a create within a foreach. However you can merge and presumably accomplish the same as originally requested and

foreach ( loop in range (1,200) | merge (p1:Person {pid: loop})-[:LIVING_IN]->(a1:Address {aid:10+loop})   ) 

and so the above will find a
:Person node at pid=loop
:Address node at aid:10+loop

if they dont exist, create said nodes and then also create a relationship between said nodes. If the :Person and :Address nodes already/previously exist then we will not create additional nodes, and the same for the relationship as well

I got the same error-message with merge.

Note, that I have node-key-constraints on pid & aid ...
When I drop them and do the query with merge I will get new nodes AND relations.
Thats not what I am looking for.

I also tried with set:

Why does he try to create a new node?
I mean, if I usually do this, it works. :

Try this:

unwind [1,2,3,4,5,6,7,8,9,10] as ids
merge (p:Person {pid:ids})
merge (a:Address {aid: ids + 10})
merge (p)-[:HAS_ADDRESS]->(a)
return p, a


match (p:Person)-[:HAS_ADDRESS]->(a:Address)
return collect( as pid, collect(a.aid) as aid


1 Like

and apologies for overthinking this but rather than

unwind [1,2,3,4,5,6,7,8,9,10] ... ...


foreach ( x in range (1,200) .... ....

you can also do

unwind range (1,200) as x ... .... .....

and so thus my previous post of

unwind [1,2,3,4,5,6,7,8,9,10] as loop
match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})  
create (p1)-[:LIVING_IN]->(a1);

can become

unwind range (1,200) as loop
match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})  
create (p1)-[:LIVING_IN]->(a1);

Thank you, the last one helped :)

1 Like