Skip to content

Commit f84b29a

Browse files
committed
SQLA: Replace the deprecated lazy="noload" with lazy="raise"
SQLAlchemy 2.1 deprecated the `noload` lazy loading strategy (sqlalchemy/sqlalchemy#11045). `noload` silently returns `None`/empty collections — essentially incorrect results — and will be removed in a future release. This PR replaces all 5 occurrences of `lazy="noload"` with `lazy="raise"`, which raises `InvalidRequestError` if the relationship is accessed without an explicit eager load (e.g. `joinedload`). All affected relationships are already properly loaded via `joinedload()` wherever they're accessed, so this is a safe drop-in that also catches missing eager loads at development time instead of silently returning `None`. Two callers needed fixes to work correctly with `lazy="raise"`: - `TaskInstance.rendered_task_instance_fields` and `TaskInstance.hitl_detail` needed `passive_deletes=True` to tell SQLAlchemy to rely on the DB-level `ON DELETE CASCADE` rather than attempting ORM-level cascade processing (which would fail since FK columns are also PK columns on RTIF, and `lazy="raise"` prevents the ORM from loading the collection to clear them). - The HITL API endpoints needed `joinedload(TI.rendered_task_instance_fields)` added to their queries, since `TaskInstanceResponse` accesses `rendered_task_instance_fields` during Pydantic serialization. The `get_hitl_detail` endpoint also needed an explicit `model_validate()` call so serialization happens while the session is still active. **Changed models:** - `Log.task_instance` - `TaskInstance.rendered_task_instance_fields` - `TaskInstance.hitl_detail` - `TaskInstanceHistory.hitl_detail` - `XComModel.task` related: apache#61229
1 parent 95fc11e commit f84b29a

8 files changed

Lines changed: 78 additions & 7 deletions

File tree

‎airflow-core/src/airflow/api_fastapi/core_api/routes/public/hitl.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ def _get_task_instance_with_hitl_detail(
8484
try_number: int | None = None,
8585
) -> TI | TIH:
8686
def _query(orm_object: Base) -> TI | TIH | None:
87+
options = [joinedload(orm_object.hitl_detail)]
88+
if orm_object is TI:
89+
options.append(joinedload(TI.rendered_task_instance_fields))
8790
query = (
8891
select(orm_object)
8992
.where(
@@ -92,7 +95,7 @@ def _query(orm_object: Base) -> TI | TIH | None:
9295
orm_object.task_id == task_id,
9396
orm_object.map_index == map_index,
9497
)
95-
.options(joinedload(orm_object.hitl_detail))
98+
.options(*options)
9699
)
97100

98101
if try_number is not None:
@@ -213,7 +216,7 @@ def get_hitl_detail(
213216
map_index=map_index,
214217
try_number=None,
215218
)
216-
return task_instance.hitl_detail
219+
return HITLDetail.model_validate(task_instance.hitl_detail)
217220

218221

219222
@task_instances_hitl_router.get(
@@ -304,6 +307,7 @@ def get_hitl_details(
304307
joinedload(TI.dag_run).joinedload(DagRun.dag_model),
305308
joinedload(TI.task_instance_note),
306309
joinedload(TI.dag_version).joinedload(DagVersion.bundle),
310+
joinedload(TI.rendered_task_instance_fields),
307311
),
308312
)
309313
)

‎airflow-core/src/airflow/models/log.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Log(Base):
6363
viewonly=True,
6464
foreign_keys=[dag_id, task_id, run_id, map_index],
6565
primaryjoin="and_(Log.dag_id == TaskInstance.dag_id, Log.task_id == TaskInstance.task_id, Log.run_id == TaskInstance.run_id, Log.map_index == TaskInstance.map_index)",
66-
lazy="noload",
66+
lazy="raise",
6767
)
6868

6969
__table_args__ = (

‎airflow-core/src/airflow/models/taskinstance.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,10 @@ class TaskInstance(Base, LoggingMixin, BaseWorkload):
614614
trigger = relationship("Trigger", uselist=False, back_populates="task_instance")
615615
triggerer_job = association_proxy("trigger", "triggerer_job")
616616
dag_run = relationship("DagRun", back_populates="task_instances", lazy="joined", innerjoin=True)
617-
rendered_task_instance_fields = relationship("RenderedTaskInstanceFields", lazy="noload", uselist=False)
618-
hitl_detail = relationship("HITLDetail", lazy="noload", uselist=False)
617+
rendered_task_instance_fields = relationship(
618+
"RenderedTaskInstanceFields", lazy="raise", uselist=False, passive_deletes=True
619+
)
620+
hitl_detail = relationship("HITLDetail", lazy="raise", uselist=False, passive_deletes=True)
619621

620622
run_after = association_proxy("dag_run", "run_after")
621623
logical_date = association_proxy("dag_run", "logical_date")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class TaskInstanceHistory(Base):
131131
foreign_keys=[run_id, dag_id],
132132
)
133133

134-
hitl_detail = relationship("HITLDetailHistory", lazy="noload", uselist=False)
134+
hitl_detail = relationship("HITLDetailHistory", lazy="raise", uselist=False)
135135

136136
def __init__(
137137
self,

‎airflow-core/src/airflow/models/xcom.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class XComModel(TaskInstanceDependencies):
106106
task = relationship(
107107
"TaskInstance",
108108
viewonly=True,
109-
lazy="noload",
109+
lazy="raise",
110110
)
111111

112112
@classmethod

‎airflow-core/tests/unit/models/test_log.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import pytest
2121
from sqlalchemy import select
22+
from sqlalchemy.exc import InvalidRequestError
2223
from sqlalchemy.orm import joinedload
2324

2425
from airflow.models.log import Log
@@ -29,6 +30,27 @@
2930

3031

3132
class TestLogTaskInstanceReproduction:
33+
def test_log_task_instance_raises_without_joinedload(self, dag_maker, session):
34+
"""Accessing Log.task_instance without joinedload should raise."""
35+
with dag_maker("dag_raise_test", session=session):
36+
EmptyOperator(task_id="task_1")
37+
38+
dr = dag_maker.create_dagrun()
39+
ti = dr.get_task_instance("task_1")
40+
session.merge(ti)
41+
session.commit()
42+
43+
log = Log(event="test_event", task_instance=ti)
44+
session.add(log)
45+
session.commit()
46+
47+
session.expire_all()
48+
stmt = select(Log).where(Log.id == log.id)
49+
loaded_log = session.scalar(stmt)
50+
51+
with pytest.raises(InvalidRequestError):
52+
loaded_log.task_instance
53+
3254
def test_log_task_instance_join_correctness(self, dag_maker, session):
3355
# Create dag_1 with a task
3456
with dag_maker("dag_1", session=session):

‎airflow-core/tests/unit/models/test_taskinstance.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,6 +2750,29 @@ def test_defer_task_with_trigger_timeout(create_task_instance):
27502750
assert abs((ti.trigger_timeout - expected_timeout).total_seconds()) < 5
27512751

27522752

2753+
class TestTaskInstanceRelationships:
2754+
@pytest.mark.parametrize(
2755+
"attr",
2756+
["rendered_task_instance_fields", "hitl_detail"],
2757+
)
2758+
def test_noload_relationships_raise_without_joinedload(self, dag_maker, session, attr):
2759+
"""Accessing lazy='raise' relationships without joinedload should raise."""
2760+
from sqlalchemy.exc import InvalidRequestError
2761+
2762+
with dag_maker("test_dag", session=session):
2763+
EmptyOperator(task_id="task_1")
2764+
2765+
dr = dag_maker.create_dagrun()
2766+
ti = dr.get_task_instance("task_1")
2767+
session.merge(ti)
2768+
session.commit()
2769+
2770+
loaded_ti = session.scalar(select(TaskInstance).where(TaskInstance.id == ti.id))
2771+
2772+
with pytest.raises(InvalidRequestError):
2773+
getattr(loaded_ti, attr)
2774+
2775+
27532776
class TestTaskInstanceRecordTaskMapXComPush:
27542777
"""Test TI.xcom_push() correctly records return values for task-mapping."""
27552778

‎airflow-core/tests/unit/models/test_xcom.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,26 @@ def task_instances(session, task_instance):
126126
return task_instance, ti2 # ti2 will be cleaned up automatically with the DAG run.
127127

128128

129+
class TestXComModelRelationships:
130+
def test_xcom_task_raises_without_joinedload(self, task_instance, session):
131+
"""Accessing XComModel.task without joinedload should raise."""
132+
from sqlalchemy.exc import InvalidRequestError
133+
134+
XComModel.set(
135+
key="test_key",
136+
value="test_value",
137+
dag_id=task_instance.dag_id,
138+
task_id=task_instance.task_id,
139+
run_id=task_instance.run_id,
140+
session=session,
141+
)
142+
143+
xcom = session.scalar(select(XComModel).where(XComModel.task_id == task_instance.task_id))
144+
145+
with pytest.raises(InvalidRequestError):
146+
xcom.task
147+
148+
129149
class TestXCom:
130150
@conf_vars({("core", "xcom_backend"): "unit.models.test_xcom.CustomXCom"})
131151
def test_resolve_xcom_class(self):

0 commit comments

Comments
 (0)