Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
02-08-2023 11:23 AM - edited 02-08-2023 11:31 AM
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.
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)
Solved! Go to Solution.
02-08-2023 04:17 PM
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
02-08-2023 01:18 PM
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!
02-08-2023 01:57 PM
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)
02-08-2023 02:44 PM
It looks good. Are you having an issue?
02-08-2023 03:02 PM
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"
^}
02-08-2023 04:17 PM
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
02-08-2023 04:45 PM
Thank you for all your help.
All the sessions of the conference are now available online