Create relationship from different CSV

Hello all,
I'm using neo4j desktop version 1.6.1 and I want to import data from a SQL database into Neo4J. I exported some tables into CSV format and tried to ingest the data into Neo4j Desktop. Below are a couple of samples from those files:

  • Sample of 'companies.csv':
    id,name,country_id
    1,AAA,2
    2,BBB,3
    3,CCC,1

  • Sample of 'countries.csv':
    id,name,currency_code,country_code
    1,Sweden,SEK,SE
    2,Denmark,DKK,DK
    3,United States of America,USD,US

I have no problems creating the nodes with the following code:

// ==================== Load Country's nodes ====================
LOAD CSV WITH HEADERS FROM "file:///countries.csv" AS row
WITH
toInteger(row.id) as id,
row.name as name,
row.currency_code as currency_code,
row.country_code as country_code

MERGE (country:Country {countryID:id})
ON CREATE SET
country.name = name,
country.currency_code = currency_code,
country.country_code = country_code

RETURN COUNT(country);
// ==============================================================

// ================ Load Company's nodes ========================
LOAD CSV WITH HEADERS FROM "file:///companies.csv" AS row
WITH
toInteger(row.id) as id,
row.name as name

MERGE (company:Company {companyID:id})
ON CREATE SET
company.name = name,

RETURN COUNT(company);
// ==============================================================

My problem starts when trying to create the relationship between them as:

// ==== Create relationships between Companies and Countries ====
LOAD CSV WITH HEADERS FROM "file:///companies.csv" AS row
WITH toInteger(row.country_id) as country_id

MATCH (company:Company)
MATCH (country:Country {countryID: country_id})

MERGE (company)-[:LOCATED_IN]->(country);
// ==============================================================

The result is that all companies are "LOCATED_IN" in all countries :frowning:
What am I doing wrong, and how should I fix this? Thanks in advance for your help.
Boris

@apolo74

Desktop v 1.6.1 is interesting but more so what version of Neo4j database are you running.
Regarding your 2nd query

// ==== Create relationships between Companies and Countries ====
LOAD CSV WITH HEADERS FROM "file:///companies.csv" AS row
WITH toInteger(row.country_id) as country_id

MATCH (company:Company)
MATCH (country:Country {countryID: country_id})

MERGE (company)-[:LOCATED_IN]->(country);
// ==============================================================

line 1 read a row from the companies.csv and each row is aliased to variable row.
line 2 convert the country_id in the csv to a Integer and alias it as country_id
line 3 find me all nodes which have a label named :Company. If you have 400 :Company nodes this query returns 400 records.
line 4 find me a node with label :Country and to which the node has a property named countryID and it has a value of country_id. Presumably this returns 1 row
line 5 create of update all relationship from the node(s) found in line 3 with the node(s) found in line 4. In this case and from the example above this could result in every Company node, i.e. 400 nodes, having a relationship to 1 :Country node.

is there a reason line4 is not

Match (company:Company {companyID: row.id})
1 Like

Dear @dana_canzano,
Thanks for your help and for your detailed explanation. I changed line 4 as you suggested, and now the relationship was created in the right way. Thanks!
However, I'm still trying to understand how it is that adding {companyID: row.id} retrieves only that node that I'm interested in. I only have one week working with Neo4j, so I guess I just need to give it a bit more time until I get use to the language.
Thanks again for your help,
Boris