iosif2 opened a new pull request, #53501:
URL: https://github.com/apache/airflow/pull/53501

   ## This PR addresses issues encountered when deleting dags via both the CLI 
and the web UI.
   
   ### 1. ForeignKeyViolation on backfill table deletion
    - Reproduction Steps: first backfill any DAG. Then, execute airflow dags 
delete [dag_id]. 
    - However executing the deletion twice as separate operations resolves the 
issue and allows for successful deletion.
    - Root Cause: This error occurs randomly due to how the delete_dag function 
operates. I suspect that get_sqla_model_classes does not consistently return 
models in the correct dependency order.
   #### Command Line Output for this error:
   ```
   root@f6650f6a4c58:/opt/airflow# airflow dags delete test_dag2
   This will drop all existing records related to the specified DAG. Proceed? 
(y/n)y
   [2025-07-18T13:21:57.427+0000] {delete_dag.py:53} INFO - Deleting DAG: 
test_dag2
   Traceback (most recent call last):
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1910, in _execute_context
       self.dialect.do_execute(
     File 
"/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 
736, in do_execute
       cursor.execute(statement, parameters)
   psycopg2.errors.ForeignKeyViolation: update or delete on table "backfill" 
violates foreign key constraint "dag_run_backfill_id_fkey" on table "dag_run"
   DETAIL:  Key (id)=(2) is still referenced from table "dag_run".
   
   
   The above exception was the direct cause of the following exception:
   
   Traceback (most recent call last):
     File "/usr/local/bin/airflow", line 10, in <module>
       sys.exit(main())
     File "/opt/airflow/airflow-core/src/airflow/__main__.py", line 55, in main
       args.func(args)
     File "/opt/airflow/airflow-core/src/airflow/cli/cli_config.py", line 48, 
in command
       return func(*args, **kwargs)
     File "/opt/airflow/airflow-core/src/airflow/utils/cli.py", line 113, in 
wrapper
       return f(*args, **kwargs)
     File 
"/opt/airflow/airflow-core/src/airflow/utils/providers_configuration_loader.py",
 line 54, in wrapped_function
       return func(*args, **kwargs)
     File "/opt/airflow/airflow-core/src/airflow/cli/commands/dag_command.py", 
line 103, in dag_delete
       message = api_client.delete_dag(dag_id=args.dag_id)
     File "/opt/airflow/airflow-core/src/airflow/api/client/local_client.py", 
line 74, in delete_dag
       count = delete_dag.delete_dag(dag_id)
     File "/opt/airflow/airflow-core/src/airflow/utils/session.py", line 100, 
in wrapper
       return func(*args, session=session, **kwargs)
     File "/opt/airflow/airflow-core/src/airflow/api/common/delete_dag.py", 
line 70, in delete_dag
       count += session.execute(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", 
line 1717, in execute
       result = conn._execute_20(statement, params or {}, execution_options)
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1710, in _execute_20
       return meth(self, args_10style, kwargs_10style, execution_options)
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", 
line 334, in _execute_on_connection
       return connection._execute_clauseelement(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1577, in _execute_clauseelement
       ret = self._execute_context(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1953, in _execute_context
       self._handle_dbapi_exception(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 2134, in _handle_dbapi_exception
       util.raise_(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", 
line 211, in raise_
       raise exception
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1910, in _execute_context
       self.dialect.do_execute(
     File 
"/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 
736, in do_execute
       cursor.execute(statement, parameters)
   sqlalchemy.exc.IntegrityError: (psycopg2.errors.ForeignKeyViolation) update 
or delete on table "backfill" violates foreign key constraint 
"dag_run_backfill_id_fkey" on table "dag_run"
   DETAIL:  Key (id)=(2) is still referenced from table "dag_run".
   
   [SQL: DELETE FROM backfill WHERE backfill.dag_id = %(dag_id_1)s RETURNING 
backfill.id]
   [parameters: {'dag_id_1': 'test_dag2'}]
   (Background on this error at: https://sqlalche.me/e/14/gkpj)
   ```
   
   ### 2. ForeignKeyViolation on other tables (related to dag_version)
    - Reproduction Steps: While I am not entirely certain how to consistently 
reproduce this specific error, it appears alongside the backfill deletion error 
described above.
    - Root Cause: It suggests that deleting dag_version records is blocked 
because they are still referenced by the task_instance table.
    - Proposed Solution: I believe the RESTRICT ON DELETE behavior is 
unnecessary for this table; thus, I have modified it to CASCADE to resolve this 
issue.
   
   #### Command Line Output for this error:
   ```
   root@f6650f6a4c58:/opt/airflow/airflow-core/src/airflow# airflow dags delete 
test_dag1
   This will drop all existing records related to the specified DAG. Proceed? 
(y/n)y
   [2025-07-18T13:39:21.439+0000] {delete_dag.py:53} INFO - Deleting DAG: 
test_dag1
   Traceback (most recent call last):
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1910, in _execute_context
       self.dialect.do_execute(
     File 
"/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 
736, in do_execute
       cursor.execute(statement, parameters)
   psycopg2.errors.ForeignKeyViolation: update or delete on table "dag_version" 
violates foreign key constraint "task_instance_dag_version_id_fkey" on table 
"task_instance"
   DETAIL:  Key (id)=(01981dc1-c244-7368-bd32-44242bc82280) is still referenced 
from table "task_instance".
   
   
   The above exception was the direct cause of the following exception:
   
   Traceback (most recent call last):
     File "/usr/local/bin/airflow", line 10, in <module>
       sys.exit(main())
     File "/opt/airflow/airflow-core/src/airflow/__main__.py", line 55, in main
       args.func(args)
     File "/opt/airflow/airflow-core/src/airflow/cli/cli_config.py", line 48, 
in command
       return func(*args, **kwargs)
     File "/opt/airflow/airflow-core/src/airflow/utils/cli.py", line 113, in 
wrapper
       return f(*args, **kwargs)
     File 
"/opt/airflow/airflow-core/src/airflow/utils/providers_configuration_loader.py",
 line 54, in wrapped_function
       return func(*args, **kwargs)
     File "/opt/airflow/airflow-core/src/airflow/cli/commands/dag_command.py", 
line 103, in dag_delete
       message = api_client.delete_dag(dag_id=args.dag_id)
     File "/opt/airflow/airflow-core/src/airflow/api/client/local_client.py", 
line 74, in delete_dag
       count = delete_dag.delete_dag(dag_id)
     File "/opt/airflow/airflow-core/src/airflow/utils/session.py", line 100, 
in wrapper
       return func(*args, session=session, **kwargs)
     File "/opt/airflow/airflow-core/src/airflow/api/common/delete_dag.py", 
line 70, in delete_dag
       count += session.execute(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", 
line 1717, in execute
       result = conn._execute_20(statement, params or {}, execution_options)
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1710, in _execute_20
       return meth(self, args_10style, kwargs_10style, execution_options)
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", 
line 334, in _execute_on_connection
       return connection._execute_clauseelement(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1577, in _execute_clauseelement
       ret = self._execute_context(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1953, in _execute_context
       self._handle_dbapi_exception(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 2134, in _handle_dbapi_exception
       util.raise_(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", 
line 211, in raise_
       raise exception
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1910, in _execute_context
       self.dialect.do_execute(
     File 
"/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 
736, in do_execute
       cursor.execute(statement, parameters)
   sqlalchemy.exc.IntegrityError: (psycopg2.errors.ForeignKeyViolation) update 
or delete on table "dag_version" violates foreign key constraint 
"task_instance_dag_version_id_fkey" on table "task_instance"
   DETAIL:  Key (id)=(01981dc1-c244-7368-bd32-44242bc82280) is still referenced 
from table "task_instance".
   
   [SQL: DELETE FROM dag_version WHERE dag_version.dag_id = %(dag_id_1)s 
RETURNING dag_version.id]
   [parameters: {'dag_id_1': 'test_dag1'}]
   (Background on this error at: https://sqlalche.me/e/14/gkpj)
   ```
   
   
   <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
   
      http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
 for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party 
License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a 
newsfragment file, named `{pr_number}.significant.rst` or 
`{issue_number}.significant.rst`, in 
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
   


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