24
votes

Lets say, I have an user:

CREATE (n { name: 'Tamil' })

and 2 roles:

CREATE (n { name: 'developer' } ) 
CREATE (n { name: 'tester' } )

Then, I make relationship between the user & each of the 2 roles.

CYPHER 1.9  START a = node(*), b = node(*) 
WHERE a.name = 'Tamil' AND b.name = 'developer' 
CREATE (a)-[r:HAS_ROLE]->(b) 
RETURN r

CYPHER 1.9  START a = node(*), b = node(*) 
WHERE a.name = 'Tamil' AND b.name = 'tester' 
CREATE (a)-[r:HAS_ROLE]->(b) 
RETURN r

Now, I want to remove tester role relationship from the user. I tried:

CYPHER 1.9  START a = node:node_auto_index('name:Tamil') 
MATCH a-[r:HAS_ROLE]-() 
RETURN r

But, it returns both of the relationships. I know that i can attach property with relationships. But, again, I don't know the cypher syntax for that.

I am new to Neo4j. Any suggestions would be really great!

Thanks!

2

2 Answers

40
votes

I deleted the relationship on your original graph with this query:

START n=node(*) 
MATCH (n)-[rel:HAS_ROLE]->(r) 
WHERE n.name='Tamil' AND r.name='tester' 
DELETE rel
1
votes

I found it. I changed the relationships to have property. Like this:

CYPHER 1.9  START a = node(*), b = node(*) 
WHERE a.name = 'Tamil' AND b.name = 'developer' 
CREATE (a)-[r:HAS_ROLE {id: xyz}]->(b) 
RETURN r

CYPHER 1.9  START a = node(*), b = node(*) 
WHERE a.name = 'Tamil' AND b.name = 'tester' 
CREATE (a)-[r:HAS_ROLE {id: abc}]->(b) 
RETURN r

Then this below code deleted the specified relationship.

CYPHER 1.9  START a = node:node_auto_index('name:Tamil') 
MATCH a-[r:HAS_ROLE]-() 
WHERE r.id = abc
DELETE r;

I am not sure that this is the right way to do or not. But, it works.