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.

How to create the relationship between node by specific property?

Hello, everyone
I'm trying to create relationships between the existing Node by JsonList.

MATCH (n:Recipe) WHERE  n.id >5000 AND n.id<5040
WITH apoc.convert.fromJsonList(n.formula) AS datas, n
FOREACH (data in datas|
MERGE (:Scent{name:data.scent})-[:addDrop{drops:data.drops}]->(:Recipe{id:n.id}))

the Recipe( about n.formula) Json format is like below:

{....,
  "createdAt": "2017-04-09T05:37:12",
  **"formula": "[{"scent":"B9","drops":"1","color":"#E5127F"},{"scent":"M18","drops":"2","color":"#62B86A"},{"scent":"T24","drops":"5","color":"#F07736"},{"scent":"T44","drops":"3","color":"#84825E"},{"scent":"MA51","drops":"2","color":"#E86FA2"},{"scent":"BA69","drops":"3","color":"#84825E"}]"**,
  "id": 5001,
....
}

After I run this sctipt, I got the error :
Neo.ClientError.Schema.ConstraintValidationFailed
Node(154918) already exists with label Recipe and property id = 5001

How should I create the relationship to the existing node by specific property of the node (by id)?

3 REPLIES 3

intouch_vivek
Graph Steward

Hi @William_Lin,

Please look into below link

Hi Vivek,
Thanks a lot, I will try it.

I found I should MATCH existing node(:Recipe{id:n.id}) first, and then to connect the relationship inside FOREACH loop.

MATCH (n:Recipe) WHERE  n.id >5000 AND n.id<5040
WITH apoc.convert.fromJsonList(n.formula) AS datas, n
MATCH (q:Recipe{id:n.id})
FOREACH (data in datas|
MERGE (:Scent{name:data.scent})-[:addDrop{drops:data.drops}]->(q))

Thanks!