BairDev
BairDev

Reputation: 3221

Use CREATE, SET and add a relationship in Cypher

I have a syntax problem with the following query and I think it is impossible to do it like this:

Adding line breaks for readability, they are not in the actual query.

CREATE (a:USER:TTL {mail: '[email protected]', fullName: 'example', password: 'spgjwetpjsfj32523rmdcp23r'})
SET a += {
    createdAt : datetime.transaction('Europe/Berlin'),
    ttl : timestamp() + 172800000,
    confirmHash: 697159094 }
-[:HAS_FIRST_ADDRESS]->
(address:ADDRESS:TTL {
    addressStreet: 'ab', addressAddition: 'zusatz', addressCityCode: 12334, addressCity: 'city'})
SET address.createdAt = datetime.transaction('Europe/Berlin'), address.ttl = timestamp() + 172800000
RETURN a

This gives me an error:

Invalid input ':': expected whitespace, a variable, RelationshipsPattern, an expression or ']'

I guess that I cannot use CREATE (a) SET a+= {prop: 'a'} -[:REL]-> (b) SET b.name = 'ab' RETURN a in a row, because the query runs well when I omit the first SET part (the one for the user).

Do I need to set all properties in the parameters for the nodes like (a {prop: 'a'})? Is there any way to combine CREATE with SET and adding a relationship?

I am using Neo4J 4.2.2.

Upvotes: 0

Views: 939

Answers (1)

fbiville
fbiville

Reputation: 8970

You can rewrite your query as such:

CREATE (a:USER:TTL {
    mail: '[email protected]',
    fullName: 'example',
    password: 'spgjwetpjsfj32523rmdcp23r',
    createdAt : datetime.transaction('Europe/Berlin'),
    ttl : timestamp() + 172800000,
    confirmHash: 697159094
})-[:HAS_FIRST_ADDRESS]->(address:ADDRESS:TTL {
    addressStreet: 'ab',
    addressAddition: 'zusatz',
    addressCityCode: 12334,
    addressCity: 'city',
    createdAt: datetime.transaction('Europe/Berlin'),
    ttl: timestamp() + 172800000})
RETURN a

If you really need those SET clauses, remember that SET can only be used for setting properties and labels, not relationships to nodes. You need CREATE (or MERGE) to create a relationship between two nodes.

CREATE (a:USER:TTL {mail: '[email protected]', fullName: 'example', password: 'spgjwetpjsfj32523rmdcp23r'})
SET a += {
    createdAt : datetime.transaction('Europe/Berlin'),
    ttl : timestamp() + 172800000,
    confirmHash: 697159094 }
CREATE (address:ADDRESS:TTL {
    addressStreet: 'ab', addressAddition: 'zusatz', addressCityCode: 12334, addressCity: 'city'})
SET address.createdAt = datetime.transaction('Europe/Berlin'), address.ttl = timestamp() + 172800000
CREATE (a)-[:HAS_FIRST_ADDRESS]->(address)
RETURN a

Side note: labels usually follow the PascalCase case convention, not UPPERCASE (UPPERCASE is the conventional case for relationship types).

Upvotes: 2

Related Questions