cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Passing parameter to create realtionship dynamcally

myrights99
Graph Buddy

Hello:

Going thru a few documentation, I understood that parameters could not be passed to create a relationship, which I think should be possible, or I missed something in the documentation.

From the below image, I want to pass parameters to create relationships dynamically; I want to know if that is possible and how I can do that. Any help is greatly appreciated.

myrights99_0-1675884173487.png

p2id = grouped2['ID']
#pclass = df1['Class']
p2type = grouped2['Type']
p2group = grouped2['Group']
p2food = grouped2['Food']
p2allergy = grouped2['Allergy']

def create_multi_node(tx,p2id,p2type,p2group,p2food,p2allergy):
return tx.run(
"unwind range(0,size($p2id)-1) as index \
with \
$p2id[index] as p2id, \
$p2type[index] as p2type, \
$p2group[index] as p2group, \
$p2food[index] as p2food, \
$p2allergy[index] as p2allergy \
merge(p:Plant_Origin{food:p2food, id:p2id}) \
merge(alg:Allergy{allergy:p2allergy}) \
WITH p, alg \
call apoc.create.relationship(p,p2type,{},alg) \
yield rel \
return p, rel, alg \
",
p2id=p2id, p2type=p2type, p2group=p2group, p2food=p2food, p2allergy=p2allergy
)

session = driver.session(database="allergy")
#session = driver.session(database="neo4j")

# Execute the `create_mnc_node` "unit of work" within a write transaction
session.execute_write(create_multi_node, p2id=p2id, p2type=p2type, p2group=p2group, p2food=p2food, p2allergy=p2allergy)

1 ACCEPTED SOLUTION

I see. You did not pass it through the ‘with’ clause before the apoc call, so it is out of scope. Change it to:

WITH p, alg, p2type

View solution in original post

6 REPLIES 6

Yes, cypher does not allow labels nor relationship types to set dynamically.  You can use apoc procedures to overcome this.  Looks like you figured it out! 

myrights99
Graph Buddy

Thank you Ninja for your reply.

Am I missing something in passing parameters in APOC ?

p2type is the label for relationship.
call apoc.create.relationship(p,p2type,{},alg)

It looks good.  Are you having an issue? 

myrights99
Graph Buddy

I am getting the error of p2type not defined, which is not the case.

CypherSyntaxError: {code: Neo.ClientError.Statement.SyntaxError} {message: Variable `p2type` not defined (line 1, column 386 (offset: 385))
"unwind range(0,size($p2id)-1) as index with $p2id[index] as p2id, $p2type[index] as p2type, $p2group[index] as p2group, $p2food[index] as p2food, $p2allergy[index] as p2allergy merge(p:Plant_Origin{food:p2food, id:p2id}) merge(alg:Allergy{allergy:p2allergy}) WITH p, alg call apoc.create.relationship(p,p2type,{},alg) yield rel return p, rel, alg"
^}

I see. You did not pass it through the ‘with’ clause before the apoc call, so it is out of scope. Change it to:

WITH p, alg, p2type

Thank you for all your help.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online