Skip to content

Commit 5dc1555

Browse files
committed
Fix QueryBuilder.count for storage backends using sqlite
The storage backends that use sqlite instead of PostgreSQL, i.e., `core.sqlite_dos`, `core.sqlite_temp` and `core.sqlite_zip`, piggy back of the ORM models defined by the `core.psql_dos` backend by dynamically converting to the sqlite equivalent database models. The current implementation of `SqlaGroup.count` would except when used with an sqlite backend since certain columns would be ambiguously defined: sqlite3.OperationalError: ambiguous column name: db_dbgroup.id This is fixed by explicitly wrapping the classes that are joined in `sqlalchemy.orm.aliased` which will force sqlalchemy to properly alias each class removing the ambiguity.
1 parent 6a43b3f commit 5dc1555

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Diff for: aiida/storage/psql_dos/orm/groups.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,11 @@ def count(self):
116116
117117
:return: integer number of entities contained within the group
118118
"""
119+
from sqlalchemy.orm import aliased
120+
group = aliased(self.MODEL_CLASS)
121+
nodes = aliased(self.GROUP_NODE_CLASS)
119122
session = self.backend.get_session()
120-
return session.query(self.MODEL_CLASS).join(self.MODEL_CLASS.dbnodes).filter(DbGroup.id == self.pk).count()
123+
return session.query(group).join(nodes, nodes.dbgroup_id == group.id).filter(group.id == self.pk).count()
121124

122125
def clear(self):
123126
"""Remove all the nodes from this group."""

0 commit comments

Comments
 (0)