You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "krzysztofstepien-tomtom (via GitHub)" <gi...@apache.org> on 2025/06/27 16:44:25 UTC

[D] session.query is no longer supported in airflow 3.0.2 [airflow]

GitHub user krzysztofstepien-tomtom created a discussion: session.query is no longer supported in airflow 3.0.2

### Apache Airflow version

3.0.2

### If "Other Airflow 2 version" selected, which one?

_No response_

### What happened?

Hi

I am currently migrating my project from airflow 2.10.5 to airflow 3.0.2

Inside i've already have a service which manages dags state i.e
```
def _get_dag_run_state(context: Any) -> Optional[DagRunState]:
  dag_run_id = context["dag_run"].id
  event_body = Params.deserialize(context["params"], 0).event_body
  product = f"{event_body.product_line}_{event_body.product_version}"

  dag_run_state = None
  session = Session()
  try:
    state_record: DagRunMetadata = session.query(DagRunMetadata).filter_by(dag_run_id=dag_run_id, product=product).first()
    dag_run_state = state_record.state if state_record else DagRunState.UNKNOWN
  except Exception as ex:
    logging.error(f"Failed to get DagRun state for `{product}`.\n Error: {ex}")
  finally:
    session.close()
  return dag_run_state
```

However after trying to launch code in the latest airflow i am getting the following problem:
`RuntimeError: Direct database access via the ORM is not allowed in Airflow 3.0`

Is there any workaround in the latest airflow to manage dags state?

### What you think should happen instead?

_No response_

### How to reproduce

Any usage of `session.query`

### Operating System

macOS Sequoia 15.5

### Versions of Apache Airflow Providers

apache-airflow-providers-fab==2.2.1
apache-airflow-providers-cncf-kubernetes==8.4.0
apache-airflow-providers-databricks==7.4.0
apache-airflow-providers-microsoft-azure==12.4.1
apache-airflow-providers-standard==1.3.0

### Deployment

Docker-Compose

### Deployment details

_No response_

### Anything else?

_No response_

### Are you willing to submit PR?

- [ ] Yes I am willing to submit a PR!

### Code of Conduct

- [x] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)


GitHub link: https://github.com/apache/airflow/discussions/52355

----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org


Re: [D] session.query is no longer supported in airflow 3.0.2 [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
GitHub user potiuk added a comment to the discussion: session.query is no longer supported in airflow 3.0.2

Correct. Direct database access possibility has been strongly discouraged from the beginning of Airflow 2, it has been mentioned as not part of [Public Interface](https://airflow.apache.org/docs/apache-airflow/stable/public-airflow-interface.html) and [removed](https://airflow.apache.org/docs/apache-airflow/stable/release_notes.html#restricted-metadata-database-access) in Airflow 3 . You should use `task-sdk` API or REST API to interact with Airflow data model

GitHub link: https://github.com/apache/airflow/discussions/52355#discussioncomment-13599563

----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org


Re: [D] session.query is no longer supported in airflow 3.0.2 [airflow]

Posted by "krzysztofstepien-tomtom (via GitHub)" <gi...@apache.org>.
GitHub user krzysztofstepien-tomtom added a comment to the discussion: session.query is no longer supported in airflow 3.0.2

Thank you for the reply!
What do mean by `task-sdk`? Could you share some examples? Do you mean using f.e `Variable`?

GitHub link: https://github.com/apache/airflow/discussions/52355#discussioncomment-13616462

----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org


Re: [D] session.query is no longer supported in airflow 3.0.2 [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
GitHub user boring-cyborg[bot] added a comment to the discussion: session.query is no longer supported in airflow 3.0.2

Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.


GitHub link: https://github.com/apache/airflow/discussions/52355#discussioncomment-13599561

----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org


Re: [D] session.query is no longer supported in airflow 3.0.2 [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
GitHub user potiuk added a comment to the discussion: session.query is no longer supported in airflow 3.0.2

Converting it to a discussion if more is needed.

GitHub link: https://github.com/apache/airflow/discussions/52355#discussioncomment-13599564

----
This is an automatically sent email for commits@airflow.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@airflow.apache.org