From a3682eac340c48b0b7430443d52bf7af5af5cf86 Mon Sep 17 00:00:00 2001 From: Gorka Eguileor Date: Tue, 11 May 2021 17:59:46 +0200 Subject: [PATCH] SQLA 1.4: Fix calculate_resource_count SQLAlchemy 1.4 is failing on method calculate_resource_count with error: sqlalchemy.exc.ArgumentError: Query has only expression-based entities - can't find property named "XYZ_metadata". Where XYZ is backup, snapshot, and volume. This is most likely caused by SQLAlchemy not knowing what to do with the joined loads once we replace the query using `with_entities`. This patch fixes this issues by not doing unnecessary joined loads when counting resources. Closes-Bug: #1928085 Change-Id: I6b60106347aede024e5546f766453b4fec648cc2 --- cinder/db/sqlalchemy/api.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index f42bd8f84d3..49a106c9840 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -2379,7 +2379,7 @@ def calculate_resource_count(context, resource_type, filters): reason=_("Model %s doesn't support " "counting resource.") % resource_type) get_query, process_filters = CALCULATE_COUNT_HELPERS[resource_type] - query = get_query(context, session=session) + query = get_query(context, session=session, joined_load=False) if filters: query = process_filters(query, filters) if query is None: @@ -3077,10 +3077,13 @@ def snapshot_get_all(context, filters=None, marker=None, limit=None, return query.all() -def _snaps_get_query(context, session=None, project_only=False): - return model_query(context, models.Snapshot, session=session, - project_only=project_only).\ - options(joinedload('snapshot_metadata')) +def _snaps_get_query(context, session=None, project_only=False, + joined_load=True): + query = model_query(context, models.Snapshot, session=session, + project_only=project_only) + if joined_load: + query = query.options(joinedload('snapshot_metadata')) + return query @apply_like_filters(model=models.Snapshot) @@ -5263,10 +5266,14 @@ def _backup_get_all(context, filters=None, marker=None, limit=None, return query.all() -def _backups_get_query(context, session=None, project_only=False): - return model_query( +def _backups_get_query(context, session=None, project_only=False, + joined_load=True): + query = model_query( context, models.Backup, session=session, - project_only=project_only).options(joinedload('backup_metadata')) + project_only=project_only) + if joined_load: + query = query.options(joinedload('backup_metadata')) + return query @apply_like_filters(model=models.Backup)