Monica Heddneck
Monica Heddneck

Reputation: 3125

Relationship not being applied correctly between nodes in Neo4j

I am trying to run the code on this GraphGist to play with the Buendia Family Tree in Neo4j.

When I create two nodes and try to connect them with a relationship, as such:

CREATE (JoseArcadioBuendia:Male {name:'Jose Arcadio Buendia',Gender:'Male'})   

then

CREATE (UrsulaIguaran:Female {name:'Ursula Iguaran',Gender:'Female'})  

then

CREATE (JoseArcadioBuendia)-[:HUSBAND]->(UrsulaIguaran)

I get this:

enter image description here

Why is this code incorrect? The syntax looks fine and it appears to run for the author of the Gist. Am I doing something incorrectly?

Upvotes: 0

Views: 64

Answers (2)

cybersam
cybersam

Reputation: 66989

I presume that you did not include all 3 CREATE clauses in a single query.

Cypher identifiers (like JoseArcadioBuendia and UrsulaIguaran) only exist for the life of a single query. The DB does not persist these identifiers. So, if you have a query that contains just CREATE (JoseArcadioBuendia)-[:HUSBAND]->(UrsulaIguaran)), neo4j would not know that the nodes already exist and would therefore create 2 new nodes for you (as well as the relationship).

If you had put all 3 clauses in a single query, you would have seen the results you expected. Otherwise, your query would first have to use MATCH to associate those identifiers with the proper nodes (similar to @Bond's answer).

Upvotes: 2

Junbang Huang
Junbang Huang

Reputation: 1967

The third line is incorrect. You are not specifying any nodes. Try the following:

MATCH (a:Male), (b:Female)
WHERE a.name = 'Jose Arcadio Buendia' AND b.name = 'Ursula Iguaran'
CREATE (a)-[:HUSBAND]->(b);

Upvotes: 0

Related Questions