cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a relationship correctly between a parent and child node?

kasipasi
Node Clone

Hello,

I want to map parent child relationship here. I have already added a property "parent" on all child nodes indicating what code has the parent. I'm not sure how to best use that information.

TRACEPARTS:TP01 (Level2CatalogPathID) is parent to TRACEPARTS:TP01002 (Level3CatalogPathID). When I run the below code I create a relationship called parent, but when I check in the database I see parent is two ways. I want to specify parent node <-- child node relation ship in one way.

What I'm I doing wrong here?

 

//Connect Traceparts Child nodes with Parents Level3
:auto LOAD CSV WITH HEADERS from 'file:///TraceParts-Folder-Structure-Level3.csv' AS row
CALL {
    WITH row
    CALL apoc.do.when(row.Level2CatalogPathID IS NOT NULL,
        'MATCH (c:Traceparts {catalogPathID: row.Level3CatalogPathID})
        MATCH (p:Traceparts {catalogPathID: row.Level2CatalogPathID})
        MERGE (p)<-[r:parent]-(c)',
        '',
        {row:row}
    )
    YIELD value
}
IN TRANSACTIONS OF 1000 ROWS;

 

 

 

 

kasipasi_2-1664841164373.png

kasipasi_0-1664841482224.png

 

Level2CatalogPathName Level2CatalogPathID Level3CatalogPathName Level3CatalogPathID
Mechanical components TRACEPARTS:TP01 Bearings TRACEPARTS:TP01002
Mechanical components TRACEPARTS:TP01 Bearings TRACEPARTS:TP01002
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003
Mechanical components TRACEPARTS:TP01 Brakes, clutches and couplings TRACEPARTS:TP01003

kasipasi_1-1664840909281.png

 

 

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

 Don’t see how you get those results from the sample import file. Can you provide the entire file?

an alternative approach would be to use the parent identifier in each node to link them. Generally we don’t store id’s of related nodes, like a foreign key in a real action Al database, because the relationship serves that purpose. Anyways, you could try linking them as follows:

match(n:Traceparts)

where n.parent is not null

match(m:Traceparts)

where m.CatalogPathID = n.parent

merge (m)<-[:parent]-(n)

View solution in original post

4 REPLIES 4

glilienfield
Ninja
Ninja

 Don’t see how you get those results from the sample import file. Can you provide the entire file?

an alternative approach would be to use the parent identifier in each node to link them. Generally we don’t store id’s of related nodes, like a foreign key in a real action Al database, because the relationship serves that purpose. Anyways, you could try linking them as follows:

match(n:Traceparts)

where n.parent is not null

match(m:Traceparts)

where m.CatalogPathID = n.parent

merge (m)<-[:parent]-(n)

.

I looked at the spreadsheet and still don't know how you got bi-directional relationships. Also, the video is just a still frame.  Anyways, glad you were able to solve with the other query.

@glilienfield 

It worked perfectly! Thank you

// Connect Traceparts with Child nodes with Parent nodes
match(n:Traceparts)
where n.parent is not null
match(m:Traceparts)
where m.catalogPathID = n.parent
merge (m)<-[:parent]-(n)
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.