Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-11-2020 10:09 AM
Hi everybody!
I'm trying to create a Node A, and if there are b's related to a's, it should create those connections that already exist among other a's
This is what i got so far
CREATE (a:A)
WITH a
MATCH (b:B)-[:AVAILABLE]->(:A)
CREATE (a)<-[:AVAILABLE]-(b)
RETURN a
The problem with this is that it's not returning a, probably because of the create(a)
How can i do this?
Solved! Go to Solution.
12-13-2020 06:39 PM
Try this: Modified PeteM code
CREATE (a:A)
OPTIONAL MATCH (b:B)-[:AVAILABLE]->(:A)
WITH COALESCE(b) as b1, a
FOREACH(ignoreMe IN CASE WHEN b1 is not null THEN [1] ELSE [] END|
CREATE (a)<-[:AVAILABLE]-(b1)
)
RETURN a
12-11-2020 10:48 PM
Maybe:
CREATE (a:A)
MATCH (b:B)-[:AVAILABLE]->(:A)
WITH a, b
CREATE (a)<-[:AVAILABLE]-(b)
RETURN a
12-12-2020 11:13 AM
It's creating a, even if i don't have b or connection from a to b, but the response is null and i had to add with a
after the first line
12-12-2020 10:24 PM
Perhaps try with OPTIONAL MATCH (b:B)-[:AVAILABLE]->(:A)
12-13-2020 10:46 AM
I don't have b:B or :AVAILABLE yet created, so it will give an error
"Failed to create relationship
UNNAMED81, node
b is missing. If you prefer to simply ignore rows where a relationship node is missing, set 'cypher.lenient_create_relationship = true' in neo4j.conf"
12-13-2020 06:39 PM
Try this: Modified PeteM code
CREATE (a:A)
OPTIONAL MATCH (b:B)-[:AVAILABLE]->(:A)
WITH COALESCE(b) as b1, a
FOREACH(ignoreMe IN CASE WHEN b1 is not null THEN [1] ELSE [] END|
CREATE (a)<-[:AVAILABLE]-(b1)
)
RETURN a
12-15-2020 02:21 AM
Hi! This works, only had to add
with a
after the first line.
I notice that i was making a mistake, on my way of thinking this. By doing the
match (b:B)-[:AVAILABLE]->(:A)
i was getting all AVAILABLE connections from all bs, i ended up doing match only on b
12-13-2020 04:28 PM
Ok I misunderstood your problem.
I just ran the queries myself to see, and the problem is the MATCH (b:B).. query when those relationships don't exist. So the condition fails, hence nothing is returned even though node a is created.
First, since you have created the node a:A, do you really require that to be passed back?
If so, you might want to consider using CALL subquery to run the MATCH and CREATE. However this requires v4.1.0 of Neo4j since passing of variables from outer query was not supported before this.
Someone else may respond with a way around this, but I couldn't really find a different way of running the query to get the a:A node returned in this instance.
All the sessions of the conference are now available online