Write data ========== .. meta:: :description: Covers creating, merging, updating and deleting data, including dynamic property names and dynamic labels The write :ref:`clauses ` are ``CREATE``, ``MERGE``, ``SET``, ``REMOVE`` and ``DELETE``. Updates are built with ``assign`` and its siblings rather than the operators, because ``SET n.x = 1`` and the comparison ``n.x == 1`` differ by one character and mean different things. Create nodes and relationships ------------------------------ .. _whole-map: ``create`` takes a node pattern or a relationship chain. A node may carry :ref:`labels `, and a relationship renders the type and direction it carries. An inline element predicate on a write renders as written, and Neo4j decides whether the write accepts it. Put it in a ``MATCH`` clause when the filter belongs to the match. With ``properties=param("props")`` the properties render as one whole-map parameter instead of written-out entries: .. code-block:: python from cypher_dsl import Configuration, Renderer, create, node, param person = node( "Person", name="p", properties=param("props", {"email": "ada@example.com"}), ) statement = create(person).returning(person["display_name"]) renderer = Renderer.get(Configuration(server_version="5.26")) rendered = renderer.render(statement) print(rendered.cypher) print(rendered.parameters) .. code-block:: text CREATE (p:Person $props) RETURN p.display_name {'props': {'email': 'ada@example.com'}} An explicit property map renders its entries, and an entry value may be any expression: .. code-block:: python from cypher_dsl import ( Configuration, Renderer, create, lit, node, param, ) explicit = create( node("Person", name="p", properties={"email": param("email")}) ) literal = create( node("Person", name="p", properties={"email": "ada@example.com"}) ) computed = create( node( "Person", name="p", properties={"email": lit("ada") + "@example.com"}, ) ) renderer = Renderer.get(Configuration(server_version="5.26")) print(renderer.render(explicit).cypher) print(renderer.render(literal).cypher) print(renderer.render(computed).cypher) .. code-block:: cypher CREATE (p:Person {email: $email}) CREATE (p:Person {email: 'ada@example.com'}) CREATE (p:Person {email: 'ada' + '@example.com'}) The slot itself is an explicit map or a whole-map parameter. A scalar literal, a variable or a map expression such as ``map_(...)`` in the property position is rejected, because the grammar does not accept it. A variable that holds a map needs a ``SET`` operation after the create. A written relationship renders the text it carries. A relationship with no type renders ``-->``, and a ``relationship_with`` chain renders an undirected relationship in a write as well. Whether Neo4j accepts either form is its check, stated in the `CREATE manual `_. A write pattern decides reference or declaration from the name. A node whose name the preceding clauses bound, or that an earlier part of the same graph pattern already wrote, renders as the bare reference. Any other node declares its labels and property map. The reference sends neither labels nor a property map, because the occurrence is an existing value, so there is nothing for the server to compare with the earlier declaration. Endpoint legality is Neo4j's check on the text it receives. :ref:`Reusing a bound pattern ` shows the rendered forms. Merge on an explicit identity ----------------------------- ``MERGE`` searches for the pattern and creates it when it is missing. The identity is the pattern itself, never the update properties that follow, so the identity entries are written explicitly. An inline element predicate on a write renders as written, and Neo4j decides whether the write accepts it. Put the filter in a ``MATCH`` clause when it belongs to the match. A whole-map parameter in the identity renders as written: .. code-block:: python from cypher_dsl import Configuration, Renderer, assign, merge, node, param person = node("Person", name="p", properties={"email": param("email")}) statement = ( merge(person) .on_create(assign(person["created_at"], param("now"))) .returning(person) ) renderer = Renderer.get(Configuration(server_version="5.26")) print(renderer.render(statement).cypher) .. code-block:: cypher MERGE (p:Person {email: $email}) ON CREATE SET p.created_at = $now RETURN p Each merge owns at most one ``ON CREATE`` and one ``ON MATCH`` action, and each action holds any number of assignments in one call. A merged relationship may be undirected: .. code-block:: python from cypher_dsl import Configuration, Renderer, merge, node person = node("Person", name="p") movie = node("Movie", name="m") statement = merge(person.relationship_with(movie, "ACTED_IN")) renderer = Renderer.get(Configuration(server_version="5.26")) print(renderer.render(statement).cypher) .. code-block:: cypher MERGE (p:Person)-[:ACTED_IN]-(m:Movie) See the `MERGE manual `_ for how Cypher reads an undirected merge. A bulk upsert unwinds the rows first and calls ``merge`` per row. :doc:`getting-started` shows the same ``UNWIND`` shape feeding a ``create``. Set properties and labels ------------------------- ``assign`` targets one property, or an element variable whose whole property map it assigns. ``update_properties`` patches a map into an element, and ``set_labels`` adds labels: .. code-block:: python from cypher_dsl import ( Configuration, Renderer, assign, match, node, param, set_labels, update_properties, ) person = node("Person", name="p") rating = match(person).where(person["email"] == param("email")).set_( assign(person["rating"], param("rating")), set_labels(person, "Premium"), ).returning(person["display_name"]) patch = ( match(person) .set_(update_properties(person, param("changes"))) .returning(person) ) renderer = Renderer.get(Configuration(server_version="5.26")) print(renderer.render(rating).cypher) print(renderer.render(patch).cypher) .. code-block:: cypher MATCH (p:Person) WHERE p.email = $email SET p.rating = $rating, p:Premium RETURN p.display_name MATCH (p:Person) SET p += $changes RETURN p An assignment to an element variable writes the whole property map, not a replacement of the node or relationship. ``SET p = $props`` and ``SET p += $changes`` differ in how that map is applied, while a property assignment such as ``SET p.rating = $rating`` targets one entry. See the `SET manual `_ for the map rules. Remove properties and labels ---------------------------- ``remove`` takes property references and label updates. The label form uses the same ``set_labels`` node, which renders ``REMOVE`` instead of ``SET`` in this clause: .. code-block:: python from cypher_dsl import Configuration, Renderer, match, node, set_labels person = node("Person", name="p") statement = ( match(person) .remove(person["nickname"], set_labels(person, "Premium")) .returning(person) ) renderer = Renderer.get(Configuration(server_version="5.26")) print(renderer.render(statement).cypher) .. code-block:: cypher MATCH (p:Person) REMOVE p.nickname, p:Premium RETURN p Delete nodes and relationships ------------------------------ .. _element: ``delete`` and ``detach_delete`` take named elements: the node and relationship variables of the statement. A node with relationships requires the detached form: .. code-block:: python from cypher_dsl import Configuration, Renderer, match, node person = node("Person", name="p") renderer = Renderer.get(Configuration(server_version="5.26")) plain = match(person).delete(person) detached = match(person).detach_delete(person) print(renderer.render(plain).cypher) print(renderer.render(detached).cypher) .. code-block:: cypher MATCH (p:Person) DELETE p MATCH (p:Person) DETACH DELETE p The targets must name elements. An anonymous pattern or a property reference is rejected, because the clause deletes elements, not values. Write a dynamic property name ----------------------------- A subscript target keeps the key as syntax, so the property name can come from a parameter: .. code-block:: python from cypher_dsl import Configuration, Renderer, assign, match, node, param person = node("Person", name="p") written = ( match(person) .set_(assign(person[param("key")], param("value"))) .returning(person) ) removed = match(person).remove(person[param("key")]).returning(person) renderer = Renderer.get(Configuration(server_version="5.26")) print(renderer.render(written).cypher) print(renderer.render(removed).cypher) .. code-block:: cypher MATCH (p:Person) SET p[$key] = $value RETURN p MATCH (p:Person) REMOVE p[$key] RETURN p Dynamic property writes arrived in Neo4j 5.24, below this library's 5.26 minimum, so every release this library supports accepts them. Dynamic subscript reads are not subject to this write-only restriction. .. _dynamic-label-write: Write a dynamic label or type ----------------------------- ``create`` and ``merge`` take it as a node label and as a relationship type. ``set_labels`` takes it for ``SET`` and ``REMOVE``: .. code-block:: python from cypher_dsl import ( Configuration, Renderer, create, dynamic_label, match, node, param, set_labels, ) student = create( node("Student", name="s").with_labels(dynamic_label(param("school"))) ) acted = create( node("Person", name="p").relationship_to( node("Movie", name="m"), dynamic_label(param("type")) ) ) person = node("Person", name="p") promoted = match(person).set_( set_labels(person, "Premium", dynamic_label(param("label"))) ) demoted = match(person).remove( set_labels(person, dynamic_label(param("label"))) ) renderer = Renderer.get(Configuration(server_version="5.26")) print(renderer.render(student).cypher) print(renderer.render(acted).cypher) print(renderer.render(promoted).cypher) print(renderer.render(demoted).cypher) .. code-block:: cypher CREATE (s:Student:$($school)) CREATE (p:Person)-[:$($type)]->(m:Movie) MATCH (p:Person) SET p:Premium:$($label) MATCH (p:Person) REMOVE p:$($label) ``label_any()`` and ``label_all()`` write read forms. A write clause renders them as given, and the server decides whether to accept them. A read pattern combines a dynamic label with ``&`` or ``|``. See :ref:`dynamic labels `.