# Do \`with\_ancestors\` and \`with\_descendants\` work?

**URL:** <https://aiida.discourse.group/t/do-with-ancestors-and-with-descendants-work/374>\
**Category:** General Usage\
**Created:** [April 26, 2024, 6:07pm UTC](https://aiida.discourse.group/t/do-with-ancestors-and-with-descendants-work/374 "2024-04-26T18:07:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bastonero](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/bastonero/32/32_2.png) [@bastonero](https://aiida.discourse.group/u/bastonero)\
**Post date:** [April 26, 2024, 6:07pm UTC](https://aiida.discourse.group/t/do-with-ancestors-and-with-descendants-work/374/1 "2024-04-26T18:07:41Z")

</div>

Hi everyone,  
I am trying to use the QueryBuilder with these options, but they seem to produce nothing, even when having a very generic Node-Node path. This is my example:

```auto
q = QueryBuilder()
q.append(Group, filters={'label':'group_label', tag='g')
q.append(WorkChainNode, with_group='g', tag='wc')
q.append(Node, with_ancestors='wc',)

```

No results are found. Am I interpreting it the right way? I don’t see why it produces nothing whatever is the `Node` chosen (even generic `Node`).

---

<div class="post-metadata">

**Author:** ![JPchico](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/jpchico/32/29_2.png) [@JPchico](https://aiida.discourse.group/u/JPchico)\
**Post date:** [April 26, 2024, 6:22pm UTC](https://aiida.discourse.group/t/do-with-ancestors-and-with-descendants-work/374/2 "2024-04-26T18:22:08Z")

</div>

I think that the with\_ancestors and with\_descendants do not work with `WorkChain` nodes (at least it does not work for me). It does work with data nodes, and `CalcJob` nodes. I think that the explanation for why can be found [here](https://github.com/aiidateam/aiida-core/issues/4293)

---

<div class="post-metadata">

**Author:** ![bastonero](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/bastonero/32/32_2.png) [@bastonero](https://aiida.discourse.group/u/bastonero)\
**Post date:** [April 26, 2024, 7:17pm UTC](https://aiida.discourse.group/t/do-with-ancestors-and-with-descendants-work/374/3 "2024-04-26T19:17:06Z")

</div>

Thanks for pointing to this thread! It is indeed the usecase I would need.

---

<div class="post-metadata">

**Author:** ![Xing](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/xing/32/10_2.png) [@Xing](https://aiida.discourse.group/u/Xing)\
**Post date:** [April 27, 2024, 10:20am UTC](https://aiida.discourse.group/t/do-with-ancestors-and-with-descendants-work/374/4 "2024-04-27T10:20:10Z")

</div>

I had the same issue recently. `with_descendants` does not work with `WorkChain`.

---

<div class="post-metadata">

**Author:** ![mbercx](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/mbercx/32/345_2.png) [@mbercx](https://aiida.discourse.group/u/mbercx)\
**Post date:** [April 28, 2024, 1:12pm UTC](https://aiida.discourse.group/t/do-with-ancestors-and-with-descendants-work/374/5 "2024-04-28T13:12:38Z")

</div>

Indeed, the `with_ancestors` and `with_descendants` only work for the _data_ provenance, not the _logical_ provenance:

[https://aiida.readthedocs.io/projects/aiida-core/en/stable/topics/provenance/concepts.html#data-provenance-and-logical-provenance](https://aiida.readthedocs.io/projects/aiida-core/en/stable/topics/provenance/concepts.html#data-provenance-and-logical-provenance)

One critical difference between the two is that the data provenance is a directed _acyclic_ graph (DAG). The logical provenance _does_ allow for loops, for example [the `OutputInputWorkChain` example from the tutorials](https://aiida-tutorials.readthedocs.io/en/latest/sections/writing_workflows/workchain.html#exercises):

 ![image](https://global.discourse-cdn.com/free1/uploads/aiida/original/1X/ca264050ce47b4646b918c94471a54605ece283c.png)

This can create issues with infinite loops for `with_ancestors` and `with_descendants` if you could use these when querying the logical provenance, as is noted by Leonid (the OG `Querybuilder` developer, I believe) here:

> <https://github.com/aiidateam/aiida-core/issues/4305#issuecomment-674512740>
>
> Code snippet that will trigger it:
> \`\`\`
> from aiida import orm
> from aiida.commo…n.links import LinkType
> 
> data = orm.Data().store()
> calc = orm.CalculationNode().store()
> 
> data.add\_incoming(calc, link\_type=LinkType.CREATE, link\_label='label')
> 
> builder = orm.QueryBuilder()
> builder.append(orm.CalculationNode, tag='calculation')
> builder.append(orm.Data, with\_ancestors='calculation', edge\_filters={'type': LinkType.CREATE.value})
> 
> builder.count()
> \`\`\`
> the exception that is raised:
> \`\`\`
> KeyError Traceback (most recent call last)
> ~/.virtualenvs/aiida\_dev/lib/python3.7/site-packages/sqlalchemy/util/\_collections.py in \_\_getattr\_\_(self, key)
> 209 try:
> \--\> 210 return self.\_data\[key\]
> 211 except KeyError:
> 
> KeyError: 'type'
> 
> During handling of the above exception, another exception occurred:
> 
> AttributeError Traceback (most recent call last)
> ~/code/aiida/env/dev/aiida-core/aiida/orm/implementation/querybuilder.py in get\_column(self, colname, alias)
> 400 try:
> \--\> 401 return getattr(alias, colname)
> 402 except AttributeError:
> 
> ~/.virtualenvs/aiida\_dev/lib/python3.7/site-packages/sqlalchemy/util/\_collections.py in \_\_getattr\_\_(self, key)
> 211 except KeyError:
> \--\> 212 raise AttributeError(key)
> 213 
> 
> AttributeError: type
> 
> During handling of the above exception, another exception occurred:
> 
> InputValidationError Traceback (most recent call last)
> \<ipython-input-1-cb9beeb4dffe\> in \<module\>
> 11 builder.append(orm.Data, with\_ancestors='calculation', edge\_filters={'type': LinkType.CREATE.value})
> 12 
> \---\> 13 builder.count()
> 
> ~/code/aiida/env/dev/aiida-core/aiida/orm/querybuilder.py in count(self)
> 2204 :returns: the number of rows as an integer
> 2205 """
> \-\> 2206 query = self.get\_query()
> 2207 return self.\_impl.count(query)
> 2208 
> 
> ~/code/aiida/env/dev/aiida-core/aiida/orm/querybuilder.py in get\_query(self)
> 2100 
> 2101 if need\_to\_build:
> \-\> 2102 query = self.\_build()
> 2103 self.\_hash = queryhelp\_hash
> 2104 else:
> 
> ~/code/aiida/env/dev/aiida-core/aiida/orm/querybuilder.py in \_build(self)
> 1950 'The tags I know are:\\n{}'.format(tag, self.tag\_to\_alias\_map.keys())
> 1951 )
> \-\> 1952 self.\_query = self.\_query.filter(self.\_build\_filters(alias, filter\_specs))
> 1953 
> 1954 ######################### PROJECTIONS ##########################
> 
> ~/code/aiida/env/dev/aiida-core/aiida/orm/querybuilder.py in \_build\_filters(self, alias, filter\_spec)
> 1373 is\_attribute = (attr\_key or column\_name in ('attributes', 'extras'))
> 1374 try:
> \-\> 1375 column = self.\_impl.get\_column(column\_name, alias)
> 1376 except InputValidationError:
> 1377 if is\_attribute:
> 
> ~/code/aiida/env/dev/aiida-core/aiida/orm/implementation/querybuilder.py in get\_column(self, colname, alias)
> 405 'Valid columns are:\\n'.format(
> 406 colname,
> \--\> 407 alias # pylint: disable=protected-access
> 408 )
> 409 )
> 
> InputValidationError: type is not a column of \['%(140371970688720 anon)s.ancestor\_id', '%(140371970688720 anon)s.descendant\_id', 'anon\_1.depth'\]
> \`\`\`
> 
> Note that I had to adapt the exception message of \`get\_column\` itself, because it would also try to address an attribute of the \`alias\` variable, but the whole problem is that \`alias\` is not an instance of \`Alias\` but of \`sqlalchemy.sql.base.ImmutableColumnCollection\`.
> 
> @lekah I tried looking into this, but could really use your help here.

---

<div class="post-metadata">

**Author:** ![giovannipizzi](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/giovannipizzi/32/17_2.png) [@giovannipizzi](https://aiida.discourse.group/u/giovannipizzi)\
**Post date:** [May 1, 2024, 5:19am UTC](https://aiida.discourse.group/t/do-with-ancestors-and-with-descendants-work/374/6 "2024-05-01T05:19:40Z")

</div>

As Leonid mentioned in the issue, maybe this is something to be solved not with the Query Builder directly but with the Graph Explorer? Maybe easiest to discuss at the AiiDA meeting the details and then report them back here? I also am not 100% sure anymore (but the graph explorer is used internally e.g. for finding all nodes to delete if one deletes one node, etc., and is efficient for this type of recursive queries)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/aiida/original/1X/c417197d3750949351575d03ed03f5e2ad32e649.png) [@system](https://aiida.discourse.group/u/system)\
**Post date:** [May 1, 2026, 5:19am UTC](https://aiida.discourse.group/t/do-with-ancestors-and-with-descendants-work/374/7 "2026-05-01T05:19:47Z")

</div>

Closed on May 1, 2026, 5:19am UTC

This topic was automatically closed 730 days after the last reply. New replies are no longer allowed.
