Reputation: 33
I am working with Apache AGE and I am trying to create new nodes and relationships based on existing nodes and their relationships. Here is a simplified version of my queries with placeholder nodenames.
Script to reproduce on an Apache age server:
-- Create the graph
SELECT create_graph('my_graph');
-- Add NodeType1 nodes
SELECT * FROM cypher('my_graph', $$
CREATE (:NodeType1 {name: 'A', project: 'PROJECT_ID'})
CREATE (:NodeType1 {name: 'B', project: 'PROJECT_ID'})
CREATE (c:NodeType1 {name: 'C', project: 'PROJECT_ID'})
return c
$$) AS (c agtype);
-- Add NodeType2 nodes
SELECT * FROM cypher('my_graph', $$
CREATE (:NodeType2 {name: '1', project: 'PROJECT_ID'})
CREATE (b:NodeType2 {name: '2', project: 'PROJECT_ID'})
return b
$$) AS (b agtype);
-- Add relationships between NodeType1 and NodeType2
SELECT * FROM cypher('my_graph', $$
MATCH (a:NodeType1 {name: 'A', project: 'PROJECT_ID'}), (b:NodeType2 {name: '1', project: 'PROJECT_ID'})
CREATE (a)-[:RelType]->(b)
return a, b
$$) AS (a agtype, b agtype);
SELECT * FROM cypher('my_graph', $$
MATCH (a:NodeType1 {name: 'B', project: 'PROJECT_ID'}), (b:NodeType2 {name: '1', project: 'PROJECT_ID'})
CREATE (a)-[:RelType]->(b)
return a, b
$$) AS (a agtype, b agtype);
SELECT * FROM cypher('my_graph', $$
MATCH (a:NodeType1 {name: 'B', project: 'PROJECT_ID'}), (b:NodeType2 {name: '2', project: 'PROJECT_ID'})
CREATE (a)-[:RelType]->(b)
return a, b
$$) AS (a agtype, b agtype);
SELECT * FROM cypher('my_graph', $$
MATCH (a:NodeType1 {name: 'C', project: 'PROJECT_ID'}), (b:NodeType2 {name: '2', project: 'PROJECT_ID'})
CREATE (a)-[:RelType]->(b)
return a, b
$$) AS (a agtype, b agtype);
SELECT * FROM cypher('my_graph', $$
MATCH (node1:NodeType1)-[rel:RelType]->(node2:NodeType2)
WHERE NOT exists((node1)-[:RelType2]-(:NodeType3)-[:RelType2]-(node2))
AND node1.project = 'PROJECT_ID'
AND node2.project = 'PROJECT_ID'
CREATE (newNode:NodeType3 {
name: node1.name + '|' + node2.name,
uri: 'https://example.com/ns#NodeType3',
project: 'PROJECT_ID',
executionType: 'AUTO'
})
RETURN newNode
$$) AS (newNode agtype);
This query is able to find the correct nodes and create new nodes with the name a combination of both. However. I'd also like to create relationsship from the created node to the existing nodes. This does not seem to work? Below are the queries i tried:
SELECT * FROM cypher('my_graph', $$
MATCH (node1:NodeType1)-[rel:RelType]->(node2:NodeType2)
WHERE NOT exists((node1)-[:RelType2]-(:NodeType3)-[:RelType2]-(node2))
AND node1.project = 'PROJECT_ID'
AND node2.project = 'PROJECT_ID'
CREATE (newNode:NodeType3 {
name: node1.name + '|' + node2.name,
uri: 'https://example.com/ns#NodeType3',
project: 'PROJECT_ID',
executionType: 'AUTO'
})
WITH node1 AS n1, node2 AS n2, newNode AS n3
MERGE (n1)-[:RelType2 {uri: 'https://example.com/ns#RelType2'}]->(n3)
MERGE (n2)-[:RelType2 {uri: 'https://example.com/ns#RelType2'}]->(n3)
RETURN n1, n2, n3
$$) AS (n1 agtype, n2 agtype, n3 agtype);
This results in the error:
Vertex assigned to variable n3 was deleted
SELECT * FROM cypher('my_graph', $$
MATCH (node1:NodeType1)-[rel:RelType]->(node2:NodeType2)
WHERE NOT EXISTS((node1)-[:RelType2]-(:NodeType3)-[:RelType2]-(node2))
AND node1.project = 'PROJECT_ID'
AND node2.project = 'PROJECT_ID'
CREATE (newNode:NodeType3 {
name: node1.name + '|' + node2.name,
uri: 'https://example.com/ns#NodeType3',
project: 'PROJECT_ID',
executionType: 'AUTO'
})
MERGE (node1)-[:RelType2 {uri: 'https://example.com/ns#RelType2'}]->(newNode)
MERGE (node2)-[:RelType2 {uri: 'https://example.com/ns#RelType2'}]->(newNode)
RETURN node1, node2, newNode
$$) AS (node1 agtype, node2 agtype, newNode agtype);
This also results in the error:
Vertex assigned to variable newNode was deleted
WITH
clauses to alias variables.None of these approaches have resolved the issue, and we consistently encounter the vertex deletion error.
Any insights or suggestions on how to correctly create the nodes and relationships in Apache AGE would be greatly appreciated.
Upvotes: 1
Views: 48