diff --git a/ironic/db/sqlalchemy/api.py b/ironic/db/sqlalchemy/api.py index 12bb65dc32..09204d6809 100644 --- a/ironic/db/sqlalchemy/api.py +++ b/ironic/db/sqlalchemy/api.py @@ -1276,11 +1276,13 @@ class Connection(api.Connection): def destroy_portgroup(self, portgroup_id): def portgroup_not_empty(session): """Checks whether the portgroup does not have ports.""" - with _session_for_read() as session: - res = session.scalar( - sa.select( - sa.func.count(models.Port.id) - ).where(models.Port.portgroup_id == portgroup_id)) != 0 + res = session.scalar( + sa.select( + sa.func.count(models.Port.id) + ).join( + models.Portgroup, + models.Port.portgroup_id == models.Portgroup.id + ).where(models.Port.portgroup_id == portgroup_id)) != 0 return res with _session_for_write() as session: diff --git a/ironic/objects/portgroup.py b/ironic/objects/portgroup.py index ef21a5f90e..fa24b9a13b 100644 --- a/ironic/objects/portgroup.py +++ b/ironic/objects/portgroup.py @@ -285,7 +285,7 @@ class Portgroup(base.IronicObject, object_base.VersionedObjectDictCompat): :raises: PortgroupNotEmpty, PortgroupNotFound """ - self.dbapi.destroy_portgroup(self.uuid) + self.dbapi.destroy_portgroup(self.id) self.obj_reset_changes() # NOTE(xek): We don't want to enable RPC on this call just yet. Remotable diff --git a/releasenotes/notes/resolve-portgroup-deletion-failure-3b3b8b3253c0b902.yaml b/releasenotes/notes/resolve-portgroup-deletion-failure-3b3b8b3253c0b902.yaml new file mode 100644 index 0000000000..8960530528 --- /dev/null +++ b/releasenotes/notes/resolve-portgroup-deletion-failure-3b3b8b3253c0b902.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fix portgroup deletion failure by resolving id/uuid mix-up and adding a + port-to-portgroup join query.