cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Create relationship based on property

Anonymous
Not applicable

Hi Team,

I am trying to add second edge but with same relationship name between two nodes as properties is different. But I am seeing only one edge and properties related to that edge.

For example I have two rows in csv with 5 columns shown below

startf stopf rservice code    usage

340   350   Fixed    Main      Less

340   350   Fixed    Second  High

Query I am using to create database is as follows

LOAD CSV WITH HEADERS FROM 'file:///test.csv' AS aloc
MERGE (e:RadioService {label: aloc.service})
WITH e,aloc
MERGE (s:FrequencyBand {startFrequency: aloc.startf, stopFrequency: aloc.stopf})
MERGE (e)-[r:INCLUDED_IN]->(s)
SET r.Usage=aloc.usage, r.Code=aloc.code
 
The resultant output is two nodes with first row parameters and single relationship. I expect two edges as relationship properties in the second row are different. It is not showing second row properties and relationship.
 
Is there anyway to model this correctly?
Regards
Vaishali
2 REPLIES 2

glilienfield
Ninja
Ninja

When you merge, it looks to see if the pattern already exists, and only creates it if does not. In your 'merge' to create the relationship, the nodes 'e' and 's' are the same for both rows of data, AND a relation of type INCLUDED_IN already exists for the second row. As a result, a new relationship is not created. What you need to do to trigger a new relationship to get created is include the relationship properties in the 'merge' so it sees the second relations as not existing (its different since you are specifying the value of the properties that must be to match) and creates it. 

 

 

LOAD CSV WITH HEADERS FROM 'file:///test.csv' AS aloc
MERGE (e:RadioService {label: aloc.service})
MERGE (s:FrequencyBand {startFrequency: aloc.startf, stopFrequency: aloc.stopf})
MERGE (e)-[:INCLUDED_IN{Usage:aloc.usage, Code:aloc.code}]->(s)

 

 

In the above query, the INCLUDED_IN relationship doesn't match the second time because the specified properties are not the same. When you don't specify any properties, any existing relationship of that type would match. This is the same with nodes and their properties specified in the curly brackets (implicit 'where' clause with equality). 

Screen Shot 2023-01-10 at 11.47.18 PM.png

Thank you so much!!! It worked !!!