diff --git a/manila/api/views/share_network_subnets.py b/manila/api/views/share_network_subnets.py index 64e0d62b99..342ff2bec0 100644 --- a/manila/api/views/share_network_subnets.py +++ b/manila/api/views/share_network_subnets.py @@ -37,7 +37,7 @@ class ViewBuilder(common.ViewBuilder): 'id': share_network_subnet.get('id'), 'availability_zone': share_network_subnet.get('availability_zone'), 'share_network_id': share_network_subnet.get('share_network_id'), - 'share_network_name': share_network_subnet.share_network['name'], + 'share_network_name': share_network_subnet['share_network_name'], 'created_at': share_network_subnet.get('created_at'), 'segmentation_id': share_network_subnet.get('segmentation_id'), 'neutron_subnet_id': share_network_subnet.get('neutron_subnet_id'), diff --git a/manila/db/sqlalchemy/api.py b/manila/db/sqlalchemy/api.py index 312b21c450..32cc8ac134 100644 --- a/manila/db/sqlalchemy/api.py +++ b/manila/db/sqlalchemy/api.py @@ -43,6 +43,7 @@ import six from sqlalchemy import MetaData from sqlalchemy import or_ from sqlalchemy.orm import joinedload +from sqlalchemy.orm import subqueryload from sqlalchemy.sql.expression import literal from sqlalchemy.sql.expression import true from sqlalchemy.sql import func @@ -3400,7 +3401,7 @@ def _network_get_query(context, session=None): return (model_query(context, models.ShareNetwork, session=session). options(joinedload('share_instances'), joinedload('security_services'), - joinedload('share_network_subnets'))) + subqueryload('share_network_subnets'))) @require_context @@ -3543,7 +3544,7 @@ def _network_subnet_get_query(context, session=None): if session is None: session = get_session() return (model_query(context, models.ShareNetworkSubnet, session=session). - options(joinedload('share_servers'))) + options(joinedload('share_servers'), joinedload('share_network'))) @require_context diff --git a/manila/db/sqlalchemy/models.py b/manila/db/sqlalchemy/models.py index f1c0f97471..8b7fcb0206 100644 --- a/manila/db/sqlalchemy/models.py +++ b/manila/db/sqlalchemy/models.py @@ -938,7 +938,9 @@ class ShareNetwork(BASE, ManilaBase): 'ShareNetwork.id == ShareInstance.share_network_id,' 'ShareInstance.deleted == "False")') share_network_subnets = orm.relationship( - "ShareNetworkSubnet", backref='share_network', lazy='immediate', + "ShareNetworkSubnet", + lazy='joined', + backref=orm.backref('share_network', lazy='joined'), primaryjoin='and_' '(ShareNetwork.id == ShareNetworkSubnet.share_network_id,' 'ShareNetworkSubnet.deleted == "False")') @@ -990,6 +992,10 @@ class ShareNetworkSubnet(BASE, ManilaBase): def is_default(self): return self.availability_zone_id is None + @property + def share_network_name(self): + return self.share_network['name'] + class ShareServer(BASE, ManilaBase): """Represents share server used by share.""" @@ -1216,7 +1222,7 @@ class ShareGroupSnapshot(BASE, ManilaBase): status = Column(String(255)) share_group = orm.relationship( ShareGroup, - backref="snapshots", + backref=orm.backref("snapshots", lazy='joined'), foreign_keys=share_group_id, primaryjoin=('and_(' 'ShareGroupSnapshot.share_group_id == ShareGroup.id,' diff --git a/releasenotes/notes/bug-1862833-fix-backref-by-eager-loading-2d897976e7598625.yaml b/releasenotes/notes/bug-1862833-fix-backref-by-eager-loading-2d897976e7598625.yaml new file mode 100644 index 0000000000..1e3cacf8be --- /dev/null +++ b/releasenotes/notes/bug-1862833-fix-backref-by-eager-loading-2d897976e7598625.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Some resources will be eagerly loaded from the database to avoid cyclical + references and faulty results if their retrieval is deferred.