jason810496 commented on code in PR #60810:
URL: https://github.com/apache/airflow/pull/60810#discussion_r2812719704


##########
airflow-core/src/airflow/api_fastapi/execution_api/datamodels/taskinstance.py:
##########
@@ -304,6 +305,40 @@ class DagRun(StrictBaseModel):
     triggering_user_name: str | None = None
     consumed_asset_events: list[AssetEventDagRunReference]
     partition_key: str | None
+    note: str | None = None
+
+    @model_validator(mode="before")
+    @classmethod
+    def extract_dag_run_note(cls, data: Any) -> Any:
+        """Extract the `note` (`str | None` from 
`association_proxy("dag_run_note", "content")`) relationship from `DagRun` to 
prevent `DetachedInstanceError` when constructing `DagRunContext` or 
`TIRunContext` models."""
+        from sqlalchemy import inspect as sa_inspect
+        from sqlalchemy.exc import NoInspectionAvailable
+        from sqlalchemy.orm.state import InstanceState
+
+        if isinstance(data, dict):
+            return data
+
+        # Check if this is a SQLAlchemy model by looking for the inspection 
interface
+        try:
+            insp: InstanceState = sa_inspect(data)
+        except NoInspectionAvailable:
+            # Not a SQLAlchemy object, return as-is for Pydantic to handle
+            return data
+
+        # Check if dag_run_note is already loaded (avoid lazy load on detached 
instance)
+        if "note" in insp.dict:
+            note_value: str | None = insp.dict["note"]
+        else:
+            note_value = None
+
+        # Convert to dict to avoid further lazy loading issues
+        values = {
+            field_name: getattr(data, field_name, None)
+            for field_name in cls.model_fields
+            if field_name != "note"
+        }
+        values["note"] = note_value

Review Comment:
   Since we have introduced a ‎`note` field to 
‎`execution_api.datamodels.DagRun` in this PR, the ‎`dag_run: 
execution_api.datamodels.DagRun` attribute in ‎`TIRunContext` and 
‎`DagRunContext` now tries to access the ‎`note` attribute on a ‎`DagRun` SQLA 
ORM instance, which leads to CI failures such as 
   
   `
   Error extracting attribute: DetachedInstanceError: Parent instance DagRun at 
0x112bdc6a0 is not bound to a Session; lazy load operation of attribute 
dag_run_note cannot proceed (Background on this error at: 
https://sqlalche.me/e/20/bhk3) [type=get_attribute_error, input_value=DagRun 
test_dag @ 2026-0...ne. run_type: scheduled input_type=DagRun]
   `.
   
   
   
   To resolve these test errors, we could 1) let the Pydantic model respect 
SQLA instance attributes, or 2) update all the upstream SQL statements to join 
the ‎`DagRunNote` table (for example, 
‎`.options(selectinload(TI.dag_run).load_only(DagRun.note))`).
   
   We have tried option 2) before, but it required changing SQL statements in 
critical sections of the scheduler runner and other areas, which might not be 
necessary. Therefore, I decided to go with option 1), based on the PoC at 
https://github.com/jason810496/airflow/compare/fe19338…63d0095.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to