Skip to content

Commit c18ed43

Browse files
vatsrahul1001sanederchik
authored andcommitted
Fix task instance tries API returning duplicate entries for same task instance (apache#50597)
* fix task instance try endpoint query * updating TIH model
1 parent 14fe7b4 commit c18ed43

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

airflow-core/src/airflow/models/taskinstancehistory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ class TaskInstanceHistory(Base):
115115

116116
dag_run = relationship(
117117
"DagRun",
118-
primaryjoin="TaskInstanceHistory.run_id == DagRun.run_id",
118+
primaryjoin="and_(TaskInstanceHistory.run_id == DagRun.run_id, DagRun.dag_id == TaskInstanceHistory.dag_id)",
119119
viewonly=True,
120-
foreign_keys=[run_id],
120+
foreign_keys=[run_id, dag_id],
121121
)
122122

123123
def __init__(

airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_task_instances.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,28 @@ def test_should_respond_200_with_versions(self, test_client, run_id, expected_ve
19811981
},
19821982
}
19831983

1984+
def test_should_not_return_duplicate_runs(self, test_client, session):
1985+
"""
1986+
Test that ensures the task instances query doesn't return duplicates due to the updated join/filter logic.
1987+
"""
1988+
self.create_task_instances(session, task_instances=[{"state": State.SUCCESS}], with_ti_history=True)
1989+
self.create_task_instances(
1990+
session,
1991+
dag_id="example_bash_operator",
1992+
task_instances=[{"state": State.SUCCESS}],
1993+
with_ti_history=True,
1994+
)
1995+
1996+
response = test_client.get(
1997+
"/dags/example_python_operator/dagRuns/TEST_DAG_RUN_ID/taskInstances/print_the_context/tries"
1998+
)
1999+
2000+
assert response.status_code == 200
2001+
2002+
response = response.json()
2003+
2004+
assert response["total_entries"] == 2
2005+
19842006

19852007
class TestPostClearTaskInstances(TestTaskInstanceEndpoint):
19862008
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)