AmrShams07
AmrShams07

Reputation: 493

How to delete and return an edge in Age (Apache AGE) by matching its properties

I am using Age (Apache AGE), a PostgreSQL extension for graph databases. I have a graph called sample_graph with Car and Country nodes, connected by MadeIn edges. Currently, my MadeIn edges do not have any properties. I want to delete a specific edge based on the properties of the edge itself and return the deleted edge.

Here's an example of the current state of my graph:

Car {name: 'Lamborghini', madeIn: 'Italian'} -[MadeIn]-> Country {name: 'Italian'}
Car {name: 'Ford', madeIn: 'America'} -[MadeIn]-> Country {name: 'America'}
Car {name: 'Bughatti', madeIn: 'French'} -[MadeIn]-> Country {name: 'French'}

I tried the following query, but it deletes the vertex instead of the edge:

SELECT *                                                         
FROM cypher('sample_graph', $$                                                     
MATCH (a:Car)-[r:MadeIn]->(b:Country)                                           
WHERE a.name = 'Lamborghini' AND b.name = 'Italy'                               
DELETE r                                                                        
RETURN r                                                                    
$$) as (r agtype);

Upvotes: 0

Views: 149

Answers (6)

Nnaemeka Daniel John
Nnaemeka Daniel John

Reputation: 474

You seem to have made a typo in your query where you used 'Italy' instead of 'Italian' in the WHERE clause. Your updated query would be;

SELECT *
FROM cypher('sample_graph', $$
MATCH (a:Car {name: 'Lamborghini'})-[r:MadeIn]->(b:Country {name: 'Italian'}
DELETE r
RETURN r
$$) as (r agtype);

Upvotes: 0

Tito
Tito

Reputation: 268

There is a mistake in the name property for the Country node in your query. It should be Italian not Italy. Hence the correct query should be:

SELECT *                                                         
FROM cypher('sample_graph', $$                                                     
MATCH (a:Car)-[r:MadeIn]->(b:Country)                                           
WHERE a.name = 'Lamborghini' AND b.name = 'Italian'                               
DELETE r                                                                        
RETURN r                                                                    
$$) as (r agtype);

Upvotes: 0

Waleed Ahmed Shahid
Waleed Ahmed Shahid

Reputation: 151

It shouldn't delete the vertex, the query you are trying shouldn't do anything. Instead the following queries will work in those case.

For deleting vertex:

SELECT * 
FROM cypher('graph_name', $$
MATCH (Car {name: 'Lamborghini', madeIn: 'Italian'})
DELETE v
$$) as (v agtype);

For deleting all vertices and edges

SELECT * 
FROM cypher('graph_name', $$
    MATCH (Car {name: 'Lamborghini', madeIn: 'Italian'})
    DETACH DELETE v
$$) as (v agtype);

For deleting the edge only:

SELECT *                                                         
FROM cypher('sample_graph', $$                                                     
MATCH (a:Car)-[r:MadeIn]->(b:Country)                                           
WHERE a.name = 'Lamborghini' AND b.name = 'Italian'                               
DELETE r                                                                                                                                          
$$) as (r agtype);

For deleting and returning the edge:

SELECT *                                                         
FROM cypher('sample_graph', $$                                                     
MATCH (a:Car)-[r:MadeIn]->(b:Country)                                           
WHERE a.name = 'Lamborghini' AND b.name = 'Italian'                               
DELETE r                                                                        
RETURN r                                                                    
$$) as (r agtype);

Upvotes: 0

Zeeshan
Zeeshan

Reputation: 1

To delete an edge in a graph and return it filtered by a specific property, you can use the following command:

    SELECT * 
FROM cypher('graph_name', $$
    MATCH (n {name: 'Andres'})-[r:KNOWS]->()
    DELETE r
    RETURN r
$$) AS (v agtype);

In the given example, the query targets a graph named "graph_name." It looks for a node with the property "name" equal to 'Andres' and deletes the outgoing edges of type "KNOWS" from that node. The deleted edges are then returned as the result.

Also b.name = 'Italy' should be replaced with b.name = 'Italian', as per data.

Upvotes: 0

Carla
Carla

Reputation: 301

You can use the following query to delete the relation between two vertices based on the properties of the edge:

SELECT * 
FROM cypher('sample_graph', $$
    MATCH (Car {name: 'Lamborghini', madeIn: 'Italian'})-[r:MadeIn]->(Country {name: 'Italian'})
    DELETE r
    RETURN r
$$) as (r agtype);

Upvotes: 0

Panagiotis Foliadis
Panagiotis Foliadis

Reputation: 233

There is a typo in your query : WHERE a.name = 'Lamborghini' AND b.name = 'Italy'

According to the nodes in your database it should be Italian not Italy

Upvotes: 0

Related Questions