# I'm trying tocreate a constraint on a relationship without succeding

**URL:** <https://community.neo4j.com/t/im-trying-tocreate-a-constraint-on-a-relationship-without-succeding/74272>\
**Category:** Cypher\
**Tags:** cypher, indexes-constraints\
**Created:** [June 21, 2025, 10:05pm UTC](https://community.neo4j.com/t/im-trying-tocreate-a-constraint-on-a-relationship-without-succeding/74272 "2025-06-21T22:05:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paolodipietro58](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/paolodipietro58/32/21045_2.png) [@paolodipietro58](https://community.neo4j.com/u/paolodipietro58)\
**Post date:** [June 21, 2025, 10:05pm UTC](https://community.neo4j.com/t/im-trying-tocreate-a-constraint-on-a-relationship-without-succeding/74272/1 "2025-06-21T22:05:35Z")

</div>

- neo4j 5.26.7 EE, desktop version 2.0, browser version

Well,

I have a relationship `p = ()-[h:HAS_TAG]->(r:name)`

let me show you what i want to achieve:

1. `p = (n:Node)-[h:HAS_TAG]->(t1:TAG { name : "Donald" })` // which is formally CORRECT
2. `p = (n:DifferentNode)-[r:HAS_TAG]->(t1:TAG { name : "Duck" })` // which is formally CORRECT, because I want to allow different nodes to have the same attribute.name
3. `p = (n:Node)-[h:HAS_TAG]->(t1:TAG { name : "Trump" })` // which is WRONG, because I don't want to allow the same node to have more than one attribute with the same name

My constraint is:  
`CREATE CONSTRAINT IF NOT EXISTS FOR ()-[r:HAS_TAG]->(t:Tag) REQUIRE t.name IS UNIQUE ON r;`

where I mean that `Tag attributes` must be unique on the relationship `r`  
but it returns an error which for some reasons, is not as some notices I found on the documentation;

---

<div class="post-metadata">

**Author:** ![joshcornejo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/joshcornejo/32/34171_2.png) [@joshcornejo](https://community.neo4j.com/u/joshcornejo)\
**Post date:** [June 22, 2025, 7:56am UTC](https://community.neo4j.com/t/im-trying-tocreate-a-constraint-on-a-relationship-without-succeding/74272/2 "2025-06-22T07:56:15Z")

</div>

The documentation I've found doesn't mention "relationship existence constraints", the constraints it shows are only on properties on a node or on a relationship, can you point me at the documentation you used?

> **[Constraints - Cypher Manual](https://neo4j.com/docs/cypher-manual/current/constraints/)**
>
> Overview of Neo4j's constraints used for ensuring data integrity.

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [June 23, 2025, 3:51am UTC](https://community.neo4j.com/t/im-trying-tocreate-a-constraint-on-a-relationship-without-succeding/74272/3 "2025-06-23T03:51:07Z")

</div>

There is no constraint where you can specify a relationship and related nodes. The constraints are defined a node or a relationship, not a combination of the two. What you want to do is not available.

---

<div class="post-metadata">

**Author:** ![joshcornejo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/joshcornejo/32/34171_2.png) [@joshcornejo](https://community.neo4j.com/u/joshcornejo)\
**Post date:** [June 23, 2025, 7:55am UTC](https://community.neo4j.com/t/im-trying-tocreate-a-constraint-on-a-relationship-without-succeding/74272/4 "2025-06-23T07:55:33Z")

</div>

A possible way to emulate the behaviour you want with the constraints as they are:

```auto
CREATE CONSTRAINT IF NOT EXISTS FOR ()-[r:hasTag]->() REQUIRE r.tagName IS UNIQUE

```

Your `tagName` is the unique identifier of `:Node` + the property you want to be unique:

```auto
CREATE (A:Node {uuid: $nodeUuid })-[:hasTag {tagName: $nodeUuid + 'name'}]->(B:TAG {name:'4'})

```

For any node `:Node`, only 1 property with a specified property name exists.

---

<div class="post-metadata">

**Author:** ![paolodipietro58](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/paolodipietro58/32/21045_2.png) [@paolodipietro58](https://community.neo4j.com/u/paolodipietro58)\
**Post date:** [June 23, 2025, 11:54pm UTC](https://community.neo4j.com/t/im-trying-tocreate-a-constraint-on-a-relationship-without-succeding/74272/5 "2025-06-23T23:54:39Z")

</div>

Well, it could be a good idea or suggestion for the future!
