cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Creating relationships after UNWIND

I wanted to create relationships between two places after doing unwind. The code looks something like this:

// Create a place
UNWIND [
    {place: 'Westmere'},
    {place: 'Ponsonby'},
    {place: 'Greylynn'},
    {place: 'Eden Terrace'},
    {place: 'Auckland Central'},
    {place: 'Parnell'},
    {place: 'New Market'}
] AS row CREATE (toLower(replace(row.place, ' ', '')):Place {name: row.place});

CREATE (westmere)-[c:CONNECTS]->(ponsonby)
    SET c.weight = 10;
CREATE (westmere)-[c:CONNECTS]->(greylynn)
    SET c.weight = 50;
CREATE (westmere)-[c:CONNECTS]->(edenterrace)
    SET c.weight = 30;

CREATE (ponsonby)-[c:CONNECTS]->(aucklandcentral)
    SET c.weight = 10;
CREATE (ponsonby)-[c:CONNECTS]->(greylynn)
    SET c.weight = 60;
CREATE (ponsonby)-[c:CONNECTS]->(edenterrace)
    SET c.weight = 120;

CREATE (aucklandcentral)-[c:CONNECTS]->(parnell)
    SET c.weight = 8;
CREATE (aucklandcentral)-[c:CONNECTS]->(newmarket)
    SET c.weight = 62;

CREATE (parnell)-[c:CONNECTS]->(newmarket)
    SET c.weight = 45;

I get an error as:

Invalid input 'toLower': expected "(", "allShortestPaths" or "shortestPath" (line 10, column 18 (offset: 231))
"] AS row CREATE (toLower(replace(row.place, ' ', '')):Place {name: row.place});"

I am not too sure what I am doing wrong here, shouldn't the text first remove any space and then lower the text?

1 ACCEPTED SOLUTION

I believe the problem here is that you have the string replacement stuff in the wrong part of the CREATE line. This worked for me:

UNWIND [
    {place: 'Westmere'},
    {place: 'Ponsonby'},
    {place: 'Greylynn'},
    {place: 'Eden Terrace'},
    {place: 'Auckland Central'},
    {place: 'Parnell'},
    {place: 'New Market'}
] AS row 
MERGE (:Place {place: toLower(replace(row.place,' ', ''))})

And then you will want to change your relationship creations by making them like:

MATCH (westmere:Place {place: 'westmere'})
MATCH (ponsonby:Place {place: 'ponsonby'})
CREATE (westmere)-[:CONNECTS {weight: '10'}]->(ponsonby)

View solution in original post

2 REPLIES 2

I believe the problem here is that you have the string replacement stuff in the wrong part of the CREATE line. This worked for me:

UNWIND [
    {place: 'Westmere'},
    {place: 'Ponsonby'},
    {place: 'Greylynn'},
    {place: 'Eden Terrace'},
    {place: 'Auckland Central'},
    {place: 'Parnell'},
    {place: 'New Market'}
] AS row 
MERGE (:Place {place: toLower(replace(row.place,' ', ''))})

And then you will want to change your relationship creations by making them like:

MATCH (westmere:Place {place: 'westmere'})
MATCH (ponsonby:Place {place: 'ponsonby'})
CREATE (westmere)-[:CONNECTS {weight: '10'}]->(ponsonby)