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

[PR] Fix db clean dag version fk constraint [airflow]

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

   Fixes #44791
   
   ### What changes were proposed in this pull request?
   Fixed a foreign key constraint violation in the `airflow db clean` command that occurred when attempting to delete `dag_version` rows that are still referenced by active `task_instance` rows.
   
   ### Why are the changes needed?
   When running `airflow db clean` with a date that causes old `dag_version` rows (based on `created_at`) to be deleted while recent `task_instance` rows (based on `start_date`) that reference them are kept, an IntegrityError is raised due to the `task_instance_dag_version_id_fkey` RESTRICT constraint.
   
   The issue was that `dag_version` incorrectly listed `task_instance` in its `dependent_tables` configuration, suggesting a parent-child relationship that doesn't exist. In reality, `task_instance` has a foreign key TO `dag_version`, making it the referrer, not the dependent.
   
   ### How was this patch tested?
   - Added new test case `test_dag_version_not_deleted_when_task_instance_references_it` that reproduces the scenario and verifies the fix
   - The test creates an old `dag_version` and a recent `task_instance` referencing it, then runs cleanup to ensure no IntegrityError occurs
   
   ### Are there any user-facing changes?
   No, this is a bug fix that prevents an error. The behavior now correctly respects foreign key constraints.
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix db clean dag version fk constraint [airflow]

Posted by "henry3260 (via GitHub)" <gi...@apache.org>.
henry3260 commented on code in PR #59525:
URL: https://github.com/apache/airflow/pull/59525#discussion_r2624507899


##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2430,7 +2424,7 @@ def _get_num_times_stuck_in_queued(self, ti: TaskInstance, session: Session = NE
             .limit(1)
         )
 
-        query = session.query(Log).where(
+        stmt = select(func.count()).select_from(Log).where(

Review Comment:
   This change looks unrelated to this PR.



##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2440,51 +2434,41 @@ def _get_num_times_stuck_in_queued(self, ti: TaskInstance, session: Session = NE
         )
 
         if last_running_time is not None:
-            query = query.where(Log.dttm > last_running_time)
+            stmt = stmt.where(Log.dttm > last_running_time)

Review Comment:
   same



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix db clean dag version fk constraint [airflow]

Posted by "Arunodoy18 (via GitHub)" <gi...@apache.org>.
Arunodoy18 commented on PR #59525:
URL: https://github.com/apache/airflow/pull/59525#issuecomment-3677452647

   Okay i will have a look on what's wrong and catch you up with the updates.
   Thank you


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix db clean dag version fk constraint [airflow]

Posted by "henry3260 (via GitHub)" <gi...@apache.org>.
henry3260 commented on PR #59525:
URL: https://github.com/apache/airflow/pull/59525#issuecomment-3662073872

   It looks like some unrelated changes were included by mistake. Could you please remove them? Thanks!


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix db clean dag version fk constraint [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil closed pull request #59525: Fix db clean dag version fk constraint
URL: https://github.com/apache/airflow/pull/59525


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix db clean dag version fk constraint [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on PR #59525:
URL: https://github.com/apache/airflow/pull/59525#issuecomment-3711739529

   the fact that you have 2 PRs with the same pr title doesn't instil confidence in your PRs anymore.
   
   @Arunodoy18 I am going to close your PRs -- Please review and test your changes with correct PR description. Using LLMs without those increase maintenance burdens and CI run time.
   
   Feel free to recreate focussed PRs following those guidelines.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix db clean dag version fk constraint [airflow]

Posted by "Arunodoy18 (via GitHub)" <gi...@apache.org>.
Arunodoy18 commented on PR #59525:
URL: https://github.com/apache/airflow/pull/59525#issuecomment-3661938258

   In case there are any conflicts regarding the issue during the review, Do tell for the changes .
   Thank you


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org