# Create relationship at run time by concatenating two values

**URL:** <https://community.neo4j.com/t/create-relationship-at-run-time-by-concatenating-two-values/47301>\
**Category:** Cypher\
**Tags:** cypher, operations\
**Created:** [November 11, 2021, 12:05pm UTC](https://community.neo4j.com/t/create-relationship-at-run-time-by-concatenating-two-values/47301 "2021-11-11T12:05:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhanashree.murge](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dhanashree.murge/32/4568_2.png) [@dhanashree.murge](https://community.neo4j.com/u/dhanashree.murge)\
**Post date:** [November 11, 2021, 12:05pm UTC](https://community.neo4j.com/t/create-relationship-at-run-time-by-concatenating-two-values/47301/1 "2021-11-11T12:05:26Z")

</div>

I want to create relationship between nodes and relationship label should be assigned from a variable value. I am using GraphDatabase module from neo4j plugin for python for connecting to neo4j.

ex.

```auto
    def create_relationships(tx):
            result = tx.run("MATCH (a:NodeType1), (b:NodeType2)"
                            "MERGE (a)-[r:$related]->(b)"
                            "RETURN r", related = getLabelForRelationship())
            return result
```

Basically for any relation creation (a)-[:related]-(b), related string is not fixed so instead of string related I want to use a variable. Is this possible? Can someone please help me with this?

---

<div class="post-metadata">

**Author:** ![Bennu](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu/32/11659_2.png) [@Bennu](https://community.neo4j.com/u/Bennu)\
**Post date:** [November 11, 2021, 12:07pm UTC](https://community.neo4j.com/t/create-relationship-at-run-time-by-concatenating-two-values/47301/2 "2021-11-11T12:07:41Z")

</div>

Hi @dhanashree.murge !

Can you use APOC?

Try this:

> **[apoc.create.relationship - APOC Extended Documentation](https://neo4j.com/labs/apoc/4.1/overview/apoc.create/apoc.create.relationship/)**
>
> This section contains reference documentation for the apoc.create.relationship procedure.

Bennu

---

<div class="post-metadata">

**Author:** ![koji](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/koji/32/27564_2.png) [@koji](https://community.neo4j.com/u/koji)\
**Post date:** [November 11, 2021, 10:09pm UTC](https://community.neo4j.com/t/create-relationship-at-run-time-by-concatenating-two-values/47301/3 "2021-11-11T22:09:23Z")

</div>

Hi @dhanashree.murge

How about this code.

```auto
    @staticmethod
    def create_relationships(tx, related):
        result = tx.run("MATCH (a:NodeType1), (b:NodeType2) "
                        "MERGE (a)-[r:" + related + "]->(b) "
                        "RETURN r")
        return result

```

---

<div class="post-metadata">

**Author:** ![dhanashree.murge](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dhanashree.murge/32/4568_2.png) [@dhanashree.murge](https://community.neo4j.com/u/dhanashree.murge)\
**Post date:** [November 12, 2021, 6:55am UTC](https://community.neo4j.com/t/create-relationship-at-run-time-by-concatenating-two-values/47301/4 "2021-11-12T06:55:02Z")

</div>

Thank you for response. But this solution also gives similar syntax error.

error:  
SyntaxError : Invalid input ' " ', expected whitespace or a rel type name.

---

<div class="post-metadata">

**Author:** ![dhanashree.murge](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dhanashree.murge/32/4568_2.png) [@dhanashree.murge](https://community.neo4j.com/u/dhanashree.murge)\
**Post date:** [November 12, 2021, 6:55am UTC](https://community.neo4j.com/t/create-relationship-at-run-time-by-concatenating-two-values/47301/5 "2021-11-12T06:55:50Z")

</div>

Thank you @Bennu , I will try this.

---

<div class="post-metadata">

**Author:** ![koji](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/koji/32/27564_2.png) [@koji](https://community.neo4j.com/u/koji)\
**Post date:** [November 12, 2021, 7:35am UTC](https://community.neo4j.com/t/create-relationship-at-run-time-by-concatenating-two-values/47301/6 "2021-11-12T07:35:10Z")

</div>

@dhanashree.murge

This is the complete sample code.  
I modified the HelloWorldExample.  
You need to set the relationship name.

```auto
from neo4j import GraphDatabase

class HelloWorldExample:

    def __init__ (self, uri, user, password):
        self.driver = GraphDatabase.driver(uri, auth=(user, password))

    def close(self):
        self.driver.close()

    def print_relationships(self, related):
        with self.driver.session() as session:
            greeting = session.write_transaction(self.create_relationships, related)
            print(greeting)

    @staticmethod
    def create_relationships(tx, related):
        result = tx.run("MATCH (a:NodeType1), (b:NodeType2) "
                        "MERGE (a)-[r:" + related + "]->(b) "
                        "RETURN r")
        return result

if __name__ == " __main__":
    greeter = HelloWorldExample("bolt://localhost:7687", "neo4j", "hogehoge")
    greeter.print_relationships("RELNAME_LAST")
    greeter.close()

```

---

<div class="post-metadata">

**Author:** ![dhanashree.murge](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dhanashree.murge/32/4568_2.png) [@dhanashree.murge](https://community.neo4j.com/u/dhanashree.murge)\
**Post date:** [November 12, 2021, 10:00am UTC](https://community.neo4j.com/t/create-relationship-at-run-time-by-concatenating-two-values/47301/7 "2021-11-12T10:00:34Z")

</div>

Thanks a lot @koji, I was passing related variable as an argument to tx.run function so it did not work earlier. Now I treated it as separate variable as you have shown and it worked!
