amoghrajesh commented on code in PR #63916:
URL: https://github.com/apache/airflow/pull/63916#discussion_r2964129629


##########
airflow-core/src/airflow/api_fastapi/execution_api/datamodels/taskinstance.py:
##########
@@ -325,19 +330,24 @@ def extract_dag_run_note(cls, data: Any) -> Any:
             # Not a SQLAlchemy object, return as-is for Pydantic to handle
             return data
 
+        values = {}
+
+        for field_name in cls.model_fields:
+            if field_name in insp.dict:
+                values[field_name] = insp.dict[field_name]
+            elif field_name == "state" and "_state" in insp.dict:
+                values["state"] = insp.dict["_state"]
+
+        if "consumed_asset_events" not in values:
+            values["consumed_asset_events"] = []
+
         # 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
+        if "note" not in values:
+            if "dag_run_note" in insp.dict:
+                values["note"] = data.note
+            else:
+                values["note"] = None

Review Comment:
   K, let me explain.
   
   In the `DagRun` model, note is an association proxy and not a mapped column. 
The real attribute in the ORM state is `dag_run_note`.
   
   `insp.dict` reflects the mapper attributes -- so `dag_run_note`. If 
`dag_run_note` is in `insp.dict`, the relationship would have loaded and we can 
safely use `data.note` but if not, accessing `data.note` would trigger a lazy 
load and cause DetachedInstanceError on a detached instance which we want to 
prevent.
   
   So we check `dag_run_note` to know whether it’s safe to read note.



-- 
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