ak2492
ak2492

Reputation: 315

How to create relationships for all the existing nodes with cypher

I have created Employees and Companies nodes in the Neo4j database. Employees collection has foreign key [CompanyID] which is a primary key in Companies collection. Now I want to create a relationship [: WORKS_FOR ] for all the nodes. Is there a way to do this?

I tried something similar to this, but it didn't work:

MATCH (e:Employee),(c:Company)

WHERE e.companyId = c.id

CREATE (e)-[:WORKS_FOR]->(c);

Upvotes: 2

Views: 1299

Answers (1)

ak2492
ak2492

Reputation: 315

For some reason the e.companyId is stored as a string, so converting c.id to string and comparing the ids worked for me.

MATCH (e:Employee),(c:Company)

WHERE e.companyId = toString(c.id)

CREATE (e)-[:WORKS_FOR]->(c);

Upvotes: 2

Related Questions