Create relationships between nodes from different CSV

Hi all! I have a problem with my neo4j project. Basically I have loaded multiple CSVs and created nodes from them, everything's alright. But now I want to create a relationship between two nodes with some properties, and I want to relate these two nodes with two conditions:
it is a Movie<-[RATED]-User kind of relationship. I have the csv file for movie ratings that contains the user id and movie id columns, so I would like to create this relationship binding ratings(userID) with users(userID) and ratings(movieID) with movies(movieID). I know I'm not so clear, but as in sql where you can join tables on conditions like =, that's what I want to do in neo4j. Hope I've explained the concept.

Hello @fed.schiavone and welcome to the Neo4j community :slight_smile:

Can you upload the image schema of your database?

CALL db.schema.visualization()

Can you also give us the list of Labels and properties of your database?


Hi! Screenshot (42)
This is the schema. I've tried to create that relationship on my own, but I don't know if it was good:

"file:///ratings_table.csv" AS line
MATCH (m:Movie), (u:User)
WHERE m.movieId = toInteger(line.movieId) AND u.userId = toInteger(line.userId)
CREATE (u)-[:RATED {rating: toFloat(line.rating), day: toInteger(, month: toInteger(line.month), year: toInteger(line.year), movieId: toInteger(line.movieId), userId: toInteger(line.userId)}]->(m);

Anyway my labels are:

  • Genre
  • Movie
  • Tag
  • User
  • day, month, year (Of Tag node and in RATED relationship)
  • genre (Genre)
  • movieId (Movie, Genre, RATED relationship)
  • rating (RATED relationship)
  • tag (Tag)
  • title (Movie)
  • userId (User, RATED relationship)

I've stored the same properties in more than one node for creating the relationships based on the equality of those properties, like you do with foreign keys win relational db.

As far as I see, your query looks good. Did you test it?

Yes, it works, but I've had to reduce the csv file length (it's a school assignment so I don't really care a lot) because sometimes when I tested that query it went on a loop, I mean it took too long.

Did you put UNIQUE CONSTRAINTS on userId, moverId, genreId and tagId? It will speed up the load of nodes and relations :slight_smile:

1 Like

I will try! I'll let you know if it works :slight_smile: