Neo4J - Creating Relationship on existing nodes

10,818

Solution 1

Match before u create new one, as suggested in the comments!

MATCH(u:University {title:'Exeter'})
CREATE(p:Person {name:'Nick'})
CREATE(p)-[w:LIKES]->(u)
return w

Solution 2

You could also use a MERGE statement as per the docs:

MERGE either matches existing nodes and binds them, or it creates new data and binds that. It’s like a combination of MATCH and CREATE that additionally allows you to specify what happens if the data was matched or created.

You would do a query like

MERGE (p:Person {name:'Nick'})-[:LIKES]->(u:University {title:'Exeter'})
Share:
10,818
user3180997
Author by

user3180997

Updated on June 22, 2022

Comments

  • user3180997
    user3180997 almost 2 years

    I am new to Neo4J and I am looking to create a new relationship between an existing node and a new node.

    I have a university node, and person node.

    I am trying to assign a new person to an existing university.

    I am trying to following code:

    MATCH (p:Person {name:'Nick'}), (u:University {title:'Exeter'}) CREATE (p)-[:LIKES]->(u)

    So in the above code: MATCH (p:Person {name:'Nick'}) is the new user

    AND (u:University {title:'Exeter'}) is the exisiting univeristy.

    But it is coming back (no changes, no rows)

    I have even tried the query without the MATCH part but no luck either.

    I have looked at few similar answers but they didn't seem to work either.

    Any help would be very much appreciated. Thank you.