Duplicated relationships for same nodes when loading from CSV


(Gabrielsantos Ifce) #1

Hello guys,

I'm trying to load a data from a CSV file containing the following fields (header), as shown in the figure below:
vendas_cupom_subset

This CSV contains sales information of a specific store (CODIGOFILIAL). A person (CPF) can buy some products (CODIGOPRODUTO) that can be attached to the same basket (NUMEROUNICOCUPOM), which was bought in a specific date (DATAMOVIMENTO); as you can see on the highlighted text.

I'm trying to create the graph using the following cypher command:

LOAD CSV WITH HEADERS FROM 'file:///subset_vendas_cupom.csv' AS line
MERGE (n:Cliente {cpf:line.CPF})
MERGE (m:Cupom {codigoCupom:line.NUMEROUNICOCUPOM, dataMovimento:line.DATAMOVIMENTO})
MERGE (o:Produto {codigoProduto:line.CODIGOPRODUTO})
MERGE (p:Filial {filial:line.CODIGOFILIAL})
MERGE (n)-[:FEZ_PEDIDO]->(m)-[:CONTEM]->(o)-[:VENDIDO]->(p)

The problem is that it creates multiple relationships from a client to the same basket, because the basket contained several items which appear in different rows of my CSV file; as follows:

How can I make the highlighted relationships unique? Thanks in advance.


(Gabrielsantos Ifce) #2

Solved this by separating the MERGE (n)-[:FEZ_PEDIDO]->(m)-[:CONTEM]->(o)-[:VENDIDO]->(p) into small pieces.


(Ryan Boyd) #3

Great that you solved the issue.

The basic rule of thumb is that MERGE will create the entire pattern of the pattern doesn’t already exist in its entirety.