# Re-adding a relationship after deleting, doesn't work

**URL:** <https://community.neo4j.com/t/re-adding-a-relationship-after-deleting-doesnt-work/61555>\
**Category:** Cypher\
**Created:** [March 21, 2023, 12:09am UTC](https://community.neo4j.com/t/re-adding-a-relationship-after-deleting-doesnt-work/61555 "2023-03-21T00:09:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mburbidg1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mburbidg1/32/24054_2.png) [@mburbidg1](https://community.neo4j.com/u/mburbidg1)\
**Post date:** [March 21, 2023, 12:09am UTC](https://community.neo4j.com/t/re-adding-a-relationship-after-deleting-doesnt-work/61555/1 "2023-03-21T00:09:07Z")

</div>

I'm having trouble with a complex query and large dataset. For a given dataset a batch query might add, remove and re-add the same relationship between the same two nodes. This is not working for me, given my query and I can't figure out why.

Here's a whittled down query that reproduces the problem.

```auto
WITH [
    {
        entity_id: 'e41',
        component_id: 'SceneNodeChildOf',
        has_parent: true,
        parent: 'f5'
    },
    {
        entity_id: 'c57',
        component_id: 'SceneNodeChildOf',
        has_parent: true,
        parent: 'e41'
    },
    {
        entity_id: 'c57',
        component_id: 'SceneNodeChildOf',
        value: '',
        has_parent: true,
        parent: ''
    },
    {
        entity_id: 'c57',
        component_id: 'SceneNodeChildOf',
        has_parent: true,
        parent: 'e41'
    }
] AS batch
UNWIND batch as row
MERGE (e:Entity {id: row.entity_id})
WITH e as child, row
CALL {
    WITH child, row
    WITH child, row WHERE row.has_parent=True AND row.parent <> ''
    MERGE (parent:Entity {id: row.parent})
    MERGE (parent)<-[:CHILD_OF]-(child)
}
WITH child, row
CALL {
    WITH child, row
    WITH child, row WHERE row.has_parent = True AND row.parent = ''
    MATCH (child:Entity {id: row.entity_id})-[r:CHILD_OF]->(:Entity)
        DELETE r
}

```

I expect that the first item in the batch would create two nodes e41 and f5, and one CHILD\_OF relationship e41 being a child of f5.

The second item in the batch would create one new node, c57 that is a child of e41.

The third item in the batch creates no new nodes, and removes the child of relationship between c57 and e41. i.e. c57 is no longer a child of e41.

The last item in the batch should re-add the child of relationship just previously removed. But it does not. I end up with the following graph after running this query:

 ![Screen Shot 2023-03-20 at 5.05.41 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/c/6/c6e8ca969bba7f0b8cbc5b5d3db12dbcd0b2fdce.png)

If I run the same query but without the last two items in the batch, I end up with the following graph:

 ![Screen Shot 2023-03-20 at 5.07.48 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/7/9/79461c6e84e27d1d0d301d7bd18e77e515d4d49c.png)

This is what I was expecting after deleting and re-adding the relationship.

I don't understand this behavior and what is wrong with my query. Can someone give me a hand?

---

<div class="post-metadata">

**Author:** ![mburbidg1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mburbidg1/32/24054_2.png) [@mburbidg1](https://community.neo4j.com/u/mburbidg1)\
**Post date:** [March 21, 2023, 3:30am UTC](https://community.neo4j.com/t/re-adding-a-relationship-after-deleting-doesnt-work/61555/2 "2023-03-21T03:30:20Z")

</div>

After being pointed in the direction of apoc procedures to make my query cleaner, I've come up with the following query which does work as expected.

```auto
WITH [
    {
        entity_id: 'e41',
        component_id: 'SceneNodeChildOf',
        has_parent: true,
        parent: 'f5'
    },
    {
        entity_id: 'c57',
        component_id: 'SceneNodeChildOf',
        has_parent: true,
        parent: 'e41'
    },
    {
        entity_id: 'c57',
        component_id: 'SceneNodeChildOf',
        value: '',
        has_parent: true,
        parent: ''
    },
    {
        entity_id: 'c57',
        component_id: 'SceneNodeChildOf',
        has_parent: true,
        parent: 'e41'
    }
] AS batch
UNWIND batch as row
MERGE (e:Entity {id: row.entity_id})
WITH e as child, row
CALL {
    WITH child, row
    CALL apoc.do.case([
        row.has_parent = True AND row.parent <> '',
        'MERGE (parent:Entity {id: row.parent})
         MERGE (parent)<-[:CHILD_OF]-(child)
         RETURN child',
        row.has_parent = True AND row.parent = '',
        'MATCH (child:Entity {id: row.entity_id})-[r:CHILD_OF]->(:Entity)
         DELETE r
         RETURN child'
    ],
    'RETURN child', {child:child, row:row})
    YIELD value as entity
}

```

Though I would still like to understand why my original query didn't work as expected. I'm using a sub-query here because there is more to my query, when it is not whittled down to this reproducible example.

---

<div class="post-metadata">

**Author:** ![stuart.laurie1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/stuart.laurie1/32/18348_2.png) [@stuart.laurie1](https://community.neo4j.com/u/stuart.laurie1)\
**Post date:** [March 22, 2023, 6:43pm UTC](https://community.neo4j.com/t/re-adding-a-relationship-after-deleting-doesnt-work/61555/3 "2023-03-22T18:43:37Z")

</div>

Hi @mburbidg1 getting back to this

Cypher always executes each clause completely, before going to the next.`UNWIND` produces a table with a `row` column. **Each** row of that table is fed into `MERGE` clause, which performs side effects and introduces `e` column to the intermediate results table.

That table is then **fully** processed by they first `CALL {}` clause, which also performs updates.

Then the intermediate results table is processed by the next `CALL {}` clause.

There is no going back and forth between clauses, it's all linear. The rule to remember is that preceding clauses **can never** see updates made by later clauses. In this case, the first `CALL {}` clause cannot possibly see and `DELETE` performed by the second `CALL {}` .

More on [clause composition](https://neo4j.com/docs/cypher-manual/current/introduction/clause_composition/)
