Reputation: 107
I have two nodes named Room(4) and Houses(4). They have been created in the following way:
CREATE (n:Room { code: 1})
CREATE (n:Room { code: 1})
CREATE (n:Room { code: 1})
CREATE (n:Room { code: 1})
CREATE (n:House { code: 1})
CREATE (n:House { code: 2})
CREATE (n:House { code: 3})
CREATE (n:House { code: 4})
These are some of the relations that i am trying to create between the nodes
MATCH (room:Room), (house:House{code:1})
WHERE id(room) = 40
CREATE UNIQUE (room)-[:PLACED_IN]->(house) ;
MATCH (room:Room), (house:House{code:2})
WHERE id(room) = 41
CREATE UNIQUE (room)-[:PLACED_IN]->(house) ;
MATCH (room:Room), (house:House{code:3})
WHERE id(room) = 42
CREATE UNIQUE (room)-[:PLACED_IN]->(house) ;
The ID's have not been defined before so it should be creating new rooms based on ID's or should i add the ID's manually while creating as currently the relationships are not being created due to WHERE clause?
Upvotes: 1
Views: 682
Reputation: 16365
Change your query to:
// match room by internal id
MATCH (room:Room)
WHERE id(room) = 40
// merge will create a relationship between `room.id = 40`
// and `house.code = 1`. If `house.code = 1` does not exists, it will be created
MERGE (room)-[:PLACED_IN]->(:House {code:1}) ;
MATCH (room:Room)
WHERE id(room) = 41
MERGE (room)-[:PLACED_IN]->(:House {code:2}) ;
MATCH (room:Room)
WHERE id(room) = 42
MERGE (room)-[:PLACED_IN]->(:House {code:3}) ;
Some tips:
Avoid depending on Neo4j internal IDs
because the are not safe. Neo4j
reuses these IDs when nodes and relationships are deleted.
CREATE UNIQUE
is deprecated. Use MERGE
instead.
Upvotes: 1