18.9 Python APIs for Executing UPDATE Queries

You can update a graph that is loaded into the graph server (PGX) using the Python APIs.

However, prior to updating the graph, you must first clone the graph. You can perform update operations only on the cloned graph and not on the original graph.
The following example shows the steps for running UPDATE queries against a graph in the graph server (PGX) using the Python APIs.
  1. Load the PGQL property graph into the graph server (PGX).
    >>> g1 = session.read_graph_by_name('BANK_GRAPH', 'pg_pgql')
  2. Clone the graph for the update operation.
    >>> g2 = g1.clone(name="BANK_GRAPH_NEW")
  3. Update the cloned graph as required.
    For example:
    • Adding one or more vertices with properties
      >>> g2.execute_pgql(
      ...   "INSERT VERTEX v1 LABELS (Accounts) PROPERTIES (v1.id=1001, v1.name='New account-1') "
      ...   "     , VERTEX v2 LABELS (Accounts) PROPERTIES (v2.id=1002, v2.name='New account-2') "
      ... )
    • Inserting a new edge with properties

      The new edge gets added between all vertices that match the WHERE clause.

      >>> g2.execute_pgql(
      ...   "INSERT EDGE e1 BETWEEN v1 AND v2 "
      ...   "LABELS (Transfers) "
      ...   "PROPERTIES (e1.from_acct_id=1001, e1.amount=3000, e1.description='Transaction-A', e1.to_acct_id=1002) "
      ...   "FROM MATCH (v1:Accounts), MATCH (v2:Accounts) "
      ...   "WHERE v1.id=1001 AND v2.id=1002"
      ...   )
      

      Optionally, query the graph to verify that the new edge is added.

      >>> g2.execute_pgql(
      ...   "SELECT e.* FROM MATCH (v1:Accounts) -[e:Transfers]-> (v2:Accounts) "
      ...   "WHERE v1.id=1001 AND v2.id=1002"
      ... ).print()
      +----------------------------------------------------+
      | FROM_ACCT_ID | TO_ACCT_ID | DESCRIPTION   | AMOUNT |
      +----------------------------------------------------+
      | 1001         | 1002       | Transaction-A | 3000.0 |
      +----------------------------------------------------+
    • Updating one or more vertex property

      The vertex properties get updated for all vertices that match the WHERE clause.

      >>> g2.execute_pgql(
      ...   "UPDATE v SET (v.name='Account-1001') "
      ...   "FROM MATCH (v:Accounts) "
      ...   "WHERE v.id=1001"
      ... )
    • Updating one or more edge property

      The edge properties get updated for the edge that connects the vertices in the WHERE clause.

      >>> g2.execute_pgql(
      ...   "UPDATE e SET (e.amount=5000) "
      ...   "FROM MATCH (v1:Accounts) -[e:Transfers]-> (v2:Accounts) "
      ...   "WHERE v1.id=1001 AND v2.id=1002"
      ... )

      Optionally, query the graph to verify the updated edge property.

      >>> g2.execute_pgql(
      ...   "SELECT e.amount FROM MATCH (v1:Accounts) -[e:Transfers]-> (v2:Accounts) "
      ...   "WHERE v1.id=1001 AND v2.id=1002"
      ... ).print()
      +--------+
      | amount |
      +--------+
      | 5000.0 |
      +--------+
    • Deleting a vertex

      Note that when you delete a vertex, all edges that connect the vertex are also removed.

      >>> g2.execute_pgql("DELETE v FROM MATCH (v:Accounts) WHERE v.id=1001")
    Alternatively, you can combine step-2 and step-3 by using the clone_and_execute_pgql() method as shown:
    >>> g2 = g1.clone_and_execute_pgql(
    ...   "INSERT VERTEX v1 LABELS (Accounts) PROPERTIES (v1.id=1001, v1.name='New account-1') "
    ...   ",  VERTEX v2 LABELS (Accounts) PROPERTIES (v2.id=1002, v2.name='New account-2') "
    ...   ",  EDGE e1 BETWEEN v1 AND v2 LABELS (Transfers) PROPERTIES (e1.amount=3000) "
    ... )

    Optionally, query the graph to verify the newly added edge.

    >>> g2.execute_pgql(
    ...   "SELECT e.amount FROM MATCH (v1:Accounts) -[e:Transfers]-> (v2:Accounts) "
    ...   "WHERE v1.id=1001 AND v2.id=1002"
    ... ).print()
    +--------+
    | amount |
    +--------+
    | 3000.0 |
    +--------+

Executing UPDATE Queries Against a PgxSession

You can also run UPDATE queries against a PgxSession as shown:

>>> g1 = session.read_graph_by_name('BANK_GRAPH', 'pg_pgql')
>>> g2 = g1.clone(name="BANK_GRAPH_NEW")
>>> session.execute_pgql(
...   "INSERT INTO BANK_GRAPH_NEW VERTEX v1 LABELS (Accounts) PROPERTIES (v1.id=1001, v1.name='New account-1') "
...   ",  VERTEX v2 LABELS (Accounts) PROPERTIES (v2.id=1002, v2.name='New account-2') "
...   ",  EDGE e1 BETWEEN v1 AND v2 LABELS (Transfers) PROPERTIES (e1.amount=3000) "
... )
>>> session.execute_pgql(
...   "SELECT e.amount FROM MATCH (v1:Accounts) -[e:Transfers]-> (v2:Accounts) ON BANK_GRAPH_NEW "
...   "WHERE v1.id=1001 AND v2.id=1002"
... ).print()
+--------+
| amount |
+--------+
| 3000.0 |
+--------+