Chris vCB
Chris vCB

Reputation: 1053

neo4j: creating the same relationship towards multiple nodes using neo4j-restclient

Using the Python neo4j-restclient, I'm trying to find out if I can simplify the creation of the same relationship emanating from the same node to different nodes. So currently, I have

alice = g.nodes.create(name='Alice')
bob = g.nodes.create(name='Bob')
chuck = g.nodes.create(name='Chuck')
darryl = g.nodes.create(name='Darryl')
eve = g.nodes.create(name='Eve')

alice.relationships.create("is friends with", bob)
alice.relationships.create("is friends with", chuck)
alice.relationships.create("is friends with", darryl)
alice.relationships.create("is friends with", eve)

Is there any simpler way to do this, without having to invoke relationships.create a dozen times?

Upvotes: 0

Views: 277

Answers (1)

Javier de la Rosa
Javier de la Rosa

Reputation: 669

I would use a Cypher query or a batch-based transaction for that.

with g.transaction():
    alice = g.nodes.create(name='Alice')
    for name in ["Bob","Chuck","Darryl","Eve"]:
        friend = g.nodes.create(name=name)
        alice.relationships.create("is friends with", friend)

Or even without the transaction.

Upvotes: 0

Related Questions