# Online training Creating Relationships. Got confused with Check your understanding (question 3)

**URL:** <https://community.neo4j.com/t/online-training-creating-relationships-got-confused-with-check-your-understanding-question-3/32952>\
**Category:** Graph Academy\
**Tags:** cypher\
**Created:** [January 30, 2021, 6:22pm UTC](https://community.neo4j.com/t/online-training-creating-relationships-got-confused-with-check-your-understanding-question-3/32952 "2021-01-30T18:22:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ildarcheg](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ildarcheg/32/9483_2.png) [@ildarcheg](https://community.neo4j.com/u/ildarcheg)\
**Post date:** [January 30, 2021, 6:22pm UTC](https://community.neo4j.com/t/online-training-creating-relationships-got-confused-with-check-your-understanding-question-3/32952/1 "2021-01-30T18:22:48Z")

</div>

Hi there. Got confused with question 3:

Assuming the nodes are successfully retrieved, how many relationships are created?  
MATCH (a:Person), (m:Movie)  
WHERE a.name = 'Tom Jones' AND m.title = 'Life is Wonderful'  
CREATE (a)-[rel:ACTED\_IN]-\>(m)  
CREATE (a)-[rel:ACTED\_IN {roles: ['The Villain']}]-\>(m)  
CREATE (a)-[rel:ACTED\_IN {roles: ['The Villain','Joe']}]-\>(m)

The right answer is 3. But when I ran the code I got error: Variable `rel` already declared (line 4, column 13 (offset: 132))  
"CREATE (a)-[rel:ACTED\_IN {roles: ['The Villain']}]-\>(m)"

So basically this code creates 0 relationships, not 3. Correct?

---

<div class="post-metadata">

**Author:** ![sam\_gijare](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/sam_gijare/32/7692_2.png) [@sam\_gijare](https://community.neo4j.com/u/sam_gijare)\
**Post date:** [January 31, 2021, 10:01pm UTC](https://community.neo4j.com/t/online-training-creating-relationships-got-confused-with-check-your-understanding-question-3/32952/2 "2021-01-31T22:01:15Z")

</div>

If All the relationships are specified and is already retrieved then nothing needs to be created but this is conditional. If something is removed/deleted or detached then the entity that is removed/deleted/detached needs to be reattached or (created again and attached).

---

<div class="post-metadata">

**Author:** ![clem](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/clem/32/4364_2.png) [@clem](https://community.neo4j.com/u/clem)\
**Post date:** [February 1, 2021, 4:48pm UTC](https://community.neo4j.com/t/online-training-creating-relationships-got-confused-with-check-your-understanding-question-3/32952/3 "2021-02-01T16:48:36Z")

</div>

Variables are a bit tricky in Neo4J.

They get created when first referenced in CREATE or MATCH, so that they can be used later.

The problem (I'm guessing an oversight when creating the question), is that the second `CREATE`:

`CREATE (a)-[rel:ACTED_IN {roles: ['The Villain']}]->(m)`

is implicitly trying to create a new variable `rel` (as a ACTED\_IN relationship type) when `rel` was already created in the previous `CREATE` statement. This is an error!

Since the variable `rel` is never referred to again in the query, the proper Cypher code for this question is:

```auto
MATCH (a:Person), (m:Movie)
WHERE a.name = 'Tom Jones' AND m.title = 'Life is Wonderful'
CREATE (a)-[:ACTED_IN]->(m) // you could use rel one time here: CREATE (a)-[rel:ACTED_IN]->(m) 
CREATE (a)-[:ACTED_IN {roles: ['The Villain']}]->(m) // Don't use rel again!
CREATE (a)-[:ACTED_IN {roles: ['The Villain','Joe']}]->(m)

```

In which case, the answer of 3 is correct.

So, this is an error in the question. Do you have the URL?

(And good for you, for actually trying to query!)

[added]  
This is another possibility is to use three different `rel` variables (which could be used later elsewhere but aren't in this query):

```auto
MATCH (a:Person), (m:Movie)
WHERE a.name = 'Tom Jones' AND m.title = 'Life is Wonderful'
CREATE (a)-[rel1:ACTED_IN]->(m)
CREATE (a)-[rel2:ACTED_IN {roles: ['The Villain']}]->(m)
CREATE (a)-[rel3:ACTED_IN {roles: ['The Villain','Joe']}]->(m)

```

---

<div class="post-metadata">

**Author:** ![elaine\_rosenber](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/elaine_rosenber/32/27409_2.png) [@elaine\_rosenber](https://community.neo4j.com/u/elaine_rosenber)\
**Post date:** [February 2, 2021, 1:47pm UTC](https://community.neo4j.com/t/online-training-creating-relationships-got-confused-with-check-your-understanding-question-3/32952/4 "2021-02-02T13:47:27Z")

</div>

Good catch @sam_gijare !

I will update the question based upon @clem 's suggestion.

Elaine
