Skip to main content

Create nodes and relationships

You can use the methods create() and merge() to construct queries that will generate new nodes and relationships in the database.

  • create() - Creates the specified node or relationship.
  • merge() - Updates existing nodes and relationships or creates them if they don't exist.

How to create a node

To create nodes, you can just use the method node() after create():

from gqlalchemy import Create

query = Create().node(labels="Person", name="Ron").execute()

How to create a node if it doesn't exist

If you use the merge() method instead of create(), the node won't be created if it already exists, it will only be updated if the properties don't match:

from gqlalchemy import Merge

query = Merge().node(labels="Person", name="Leslie").execute()

How to create a relationship

To create relationships, you can just use the methods to() and from() after create():

from gqlalchemy import Create

query = Create()
.node(labels="Person", name="Leslie")
.to(relationship_type="FRIENDS_WITH")
.node(labels="Person", name="Ron")
.execute()