diff --git a/aiida/orm/implementation/sqlalchemy/node.py b/aiida/orm/implementation/sqlalchemy/node.py index e0b228eb3a..082fe5b025 100644 --- a/aiida/orm/implementation/sqlalchemy/node.py +++ b/aiida/orm/implementation/sqlalchemy/node.py @@ -17,7 +17,7 @@ from sqlalchemy.orm.attributes import flag_modified from aiida.backends.utils import get_automatic_user -from aiida.backends.sqlalchemy.models.node import DbNode, DbLink, DbPath +from aiida.backends.sqlalchemy.models.node import DbNode, DbLink from aiida.backends.sqlalchemy.models.comment import DbComment from aiida.backends.sqlalchemy.models.user import DbUser from aiida.backends.sqlalchemy.models.computer import DbComputer @@ -195,6 +195,7 @@ def _remove_dblink_from(self, label): def _add_dblink_from(self, src, label=None, link_type=LinkType.UNSPECIFIED): from aiida.backends.sqlalchemy import get_scoped_session + from aiida.orm.querybuilder import QueryBuilder session = get_scoped_session() if not isinstance(src, Node): raise ValueError("src must be a Node instance") @@ -217,10 +218,9 @@ def _add_dblink_from(self, src, label=None, link_type=LinkType.UNSPECIFIED): # I am linking src->self; a loop would be created if a DbPath exists # already in the TC table from self to src if link_type is LinkType.CREATE or link_type is LinkType.INPUT: - c = session.query(literal(True)).filter(DbPath.query - .filter_by(parent_id=self.dbnode.id, child_id=src.dbnode.id) - .exists()).scalar() - if c: + if QueryBuilder().append( + Node, filters={'id':self.pk}, tag='parent').append( + Node, filters={'id':src.pk}, tag='child', descendant_of='parent').count() > 0: raise ValueError( "The link you are attempting to create would generate a loop")