diff --git a/airflow/api_connexion/endpoints/import_error_endpoint.py b/airflow/api_connexion/endpoints/import_error_endpoint.py index 3d0eb052ac496..59f63c8ffb339 100644 --- a/airflow/api_connexion/endpoints/import_error_endpoint.py +++ b/airflow/api_connexion/endpoints/import_error_endpoint.py @@ -94,12 +94,11 @@ def get_import_errors( if not can_read_all_dags: # if the user doesn't have access to all DAGs, only display errors from visible DAGs readable_dag_ids = security.get_readable_dags() - query = query.join(DagModel, DagModel.fileloc == ImportErrorModel.filename).where( - DagModel.dag_id.in_(readable_dag_ids) - ) - count_query = count_query.join(DagModel, DagModel.fileloc == ImportErrorModel.filename).where( - DagModel.dag_id.in_(readable_dag_ids) + dagfiles_subq = ( + select(DagModel.fileloc).distinct().where(DagModel.dag_id.in_(readable_dag_ids)).subquery() ) + query = query.where(ImportErrorModel.filename.in_(dagfiles_subq)) + count_query = count_query.where(ImportErrorModel.filename.in_(dagfiles_subq)) total_entries = session.scalars(count_query).one() import_errors = session.scalars(query.offset(offset).limit(limit)).all() diff --git a/airflow/www/views.py b/airflow/www/views.py index 656c400eac620..b99062c551b60 100644 --- a/airflow/www/views.py +++ b/airflow/www/views.py @@ -942,9 +942,14 @@ def index(self): can_read_all_dags = get_auth_manager().is_authorized_dag(method="GET") if not can_read_all_dags: # if the user doesn't have access to all DAGs, only display errors from visible DAGs - import_errors = import_errors.join( - DagModel, DagModel.fileloc == errors.ImportError.filename - ).where(DagModel.dag_id.in_(filter_dag_ids)) + import_errors = import_errors.where( + errors.ImportError.filename.in_( + select(DagModel.fileloc) + .distinct() + .where(DagModel.dag_id.in_(filter_dag_ids)) + .subquery() + ) + ) import_errors = session.scalars(import_errors) for import_error in import_errors: