Search code examples
graph-databasesmemgraphdb

How can I create a node and a relationship in the same statement when loading a CSV file?


How can I create a node and a relationship in Memgraph in the same statement when loading a CSV file? For example:

company(short_name) ← :employed - employee (emp_id)

employee.csv looks like this:

emp_id, name

And the desired behavior would be like:

LOAD CSV FROM "o/people_nodes.csv" WITH HEADER AS row
CREATE (e:employee {emp_id: row.emp_id, name: row.name})
CREATE (c:Company {short_name: $short_name }) <- [:EMPLOYED] - (emp: Employee {emp_id : row.emp_id})

Solution

  • The query I submitted should be able to create both nodes and relationships while importing from a CSV file.

    I found a way to express it with one CREATE clause:

    LOAD CSV FROM "o/people_nodes.csv" WITH HEADER AS row 
    CREATE (c:Company {short_name: $short_name })<-[:EMPLOYED]-(e:employee {emp_id: row.emp_id, name: row.name})