Preventing Multiple Relationships between two nodes

(Soroosh Nazem) #1

Hello Everyone

I want to know how I can block eny possibility of multiple relations between two nodes. I tried to do it with MERGE, but it did not work. May you give me some hint_?

0 Likes

(Michael Black) #2

Can you provide the Cypher you're executing? Keep in mind the merge statement takes the entire line into account, not just the relationship portion.

This is will duplicate each time you execute

MERGE (a:Node)-[r:Related {mydate: new DateTime()}]->(b:Node)

This will not

MERGE (a:Node)-[r:Related]->(b:Node)
SET r.mydate = new DateTime()
1 Like

(Soroosh Nazem) #3

Hi Mike

The cypher query is:

But before that, another collegue had executed the same query by just a small difference which was EN:['Plannig','Activating'] instead of being EN:['Activating','Plannig'], then we noticed the relationship has been duplicated.
Already I made a small change and it's working, but I am going to test you method as well. My modification is:

match (s:SOLUTION {SolutionId:1}),(ar:AREA {EN:'Public Space'}) where not (s)--(ar) MERGE (s)-[:HAS_ACTION_AREA {EN:['Activating','Plannig'],IT:['Attivazione,Pianificazione'],ES:['Activando','Planificación'],PT:['Ativando','Planejamento']}]->(ar)

By the way thank you for your solution, I'll try it.

0 Likes

(Paul Drangeid) #4

Here's a little different take with an example of how I'm doing it. I'm checking a ticketing system via a JDBC call.
I want to see it the ticket is assigned to the correct Asset (there can only be one). So if someone MODIFIED the asset assigned to a ticket in the application, the JDBC data would reflect the new data, and I want to update my graph. If the data has changed, I DELETE the existing relationship
and then I create the new relationship with the matching asset.

So this isn't using cypher perse to ENFORCE a single relationship, but by design, dropping any non-matching relationship before creating a new relationship.
Because I KNOW that :Crmasset recid has a UNIQUE CONSTRAINT, I know that creating a relationship on that value will only ever produce a single MATCH.

call apoc.load.jdbc('jdbc:extendedsystems:advantage://ticketeserver:6262;catalog=//ticketeserver/CommitCRM/Db','SELECT t.RECID,t.TICKETNO,t.ASSETRECID') YIELD row
MATCH (t:Ticket {ticketnumber:row.TICKETNO})
OPTIONAL MATCH (ca:Crmasset {recid:row.ASSETRECID})
OPTIONAL MATCH (t)-[tar:TICKET_ASSET]-(ota:Crmasset)
FOREACH (ignoreMe in CASE WHEN not(exists(ota.recid)) or (row.ASSETRECID <> ota.recid)  THEN [1] ELSE [] END | DELETE tar)
FOREACH (ignoreMe in CASE WHEN exists(ca.name) THEN [1] ELSE [] END | MERGE (t)-[:TICKET_ASSET]->(ca))
0 Likes