Juliette Gia
Juliette Gia

Reputation: 148

Can two way relation be created in single CREATE query?

When I have two nodes that are connected in both directions, I use two create queries to connect them:

MATCH (london:City {name: "London"})
MATCH (dublin:City {name: "Dublin"})
CREATE (london)-[:Flight {length: 450}]->(dublin)
CREATE (dublin)-[:Flight {length: 450}]->(London);

Since the flight length is the same in both directions, can a two-way relationship be created so that I don't need two CREATE queries?

Upvotes: 1

Views: 29

Answers (1)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14371

You cannot create a 2-way relationship, every edge has a direction (although you can traverse an edge in either direction). Given not all airline routes have returns I tend to model such relationships using two edges, in case one day the return trip is removed and you have to travel a home a different way. In terms of creating the relationships, you could create them in one go using the following pattern.

MATCH (london:City {name: "London"})
MATCH (dublin:City {name: "Dublin"})
CREATE (london)-[:Flight {length: 450}]->(dublin)-[:Flight {length: 450}]->(London)

Upvotes: 1

Related Questions