diff --git a/lamindb/core/_django.py b/lamindb/core/_django.py index 1d780c8d3..4c72dfa02 100644 --- a/lamindb/core/_django.py +++ b/lamindb/core/_django.py @@ -140,18 +140,21 @@ def get_artifact_with_related( artifact, {i["featureset"]: i["slot"] for i in v} ) - m2m_any_expr = reduce( - lambda a, b: a | b, - (Q(**{f"{m2m_name}__isnull": False}) for m2m_name in m2m_relations), - ) - # this is needed to avoid querying all m2m relations even if they are all empty - # this checks if non-empty m2m relations are present in the record - m2m_any = ( - model.objects.using(artifact._state.db) - .filter(uid=artifact.uid) - .filter(m2m_any_expr) - .exists() - ) + if len(m2m_relations) == 0: + m2m_any = False + else: + m2m_any_expr = reduce( + lambda a, b: a | b, + (Q(**{f"{m2m_name}__isnull": False}) for m2m_name in m2m_relations), + ) + # this is needed to avoid querying all m2m relations even if they are all empty + # this checks if non-empty m2m relations are present in the record + m2m_any = ( + model.objects.using(artifact._state.db) + .filter(uid=artifact.uid) + .filter(m2m_any_expr) + .exists() + ) if m2m_any: m2m_data = related_data["m2m"] for m2m_name in m2m_relations: