fix(core): use upsert to prevent FK constraint violations in task DB#34977
Merged
Conversation
…aint violations INSERT OR REPLACE does DELETE + INSERT on PK conflict. The bundled SQLite has SQLITE_DEFAULT_FOREIGN_KEYS=1, so the implicit DELETE on task_details fails when child rows exist in task_history or cache_outputs. Use ON CONFLICT DO UPDATE (upsert) which updates in-place without deleting.
❌ Deploy Preview for nx-docs failed. Why did it fail? →
|
✅ Deploy Preview for nx-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Contributor
|
View your CI Pipeline Execution ↗ for commit 9ab5ecc
☁️ Nx Cloud last updated this comment at |
…aint violations INSERT OR REPLACE does DELETE + INSERT on PK conflict. The bundled SQLite has SQLITE_DEFAULT_FOREIGN_KEYS=1, so the implicit DELETE on task_details fails when child rows exist in task_history or cache_outputs. Use ON CONFLICT DO UPDATE (upsert) which updates in-place without deleting. [Self-Healing CI Rerun]
Contributor
There was a problem hiding this comment.
Nx Cloud has identified a flaky task in your failed CI:
🔂 Since the failure was identified as flaky, we triggered a CI rerun by adding an empty commit to this branch.
🔔 Heads up, your workspace has pending recommendations ↗ to auto-apply fixes for similar failures.
🎓 Learn more about Self-Healing CI on nx.dev
FrozenPandaz
approved these changes
Mar 24, 2026
FrozenPandaz
pushed a commit
that referenced
this pull request
Mar 26, 2026
…34977) ## Current Behavior `INSERT OR REPLACE` is used in `task_details` and `cache_outputs` tables. The bundled SQLite (`libsqlite3-sys`) is compiled with `SQLITE_DEFAULT_FOREIGN_KEYS=1`, so FK constraints are enforced by default. `INSERT OR REPLACE` does a DELETE + INSERT on PK conflict, and the implicit DELETE on `task_details` fails when child rows exist in `task_history` or `cache_outputs`: ``` NX DB transaction error: SqliteFailure(Error { code: ConstraintViolation, extended_code: 787 }, Some("FOREIGN KEY constraint failed")) ``` This happens because `record_task_details` is called multiple times with the same hash across different code paths (`hashTask`, `hashTasks`, `hashBatchTasks`), and by the second call, child rows already reference that hash. ## Expected Behavior Use `INSERT ... ON CONFLICT DO UPDATE` (upsert) which updates the existing row in-place without deleting it. No DELETE means no FK violation, while preserving the same idempotent behavior the code relies on. --------- Co-authored-by: nx-cloud[bot] <71083854+nx-cloud[bot]@users.noreply.github.com> (cherry picked from commit 95621cd)
Contributor
|
This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Current Behavior
INSERT OR REPLACEis used intask_detailsandcache_outputstables. The bundled SQLite (libsqlite3-sys) is compiled withSQLITE_DEFAULT_FOREIGN_KEYS=1, so FK constraints are enforced by default.INSERT OR REPLACEdoes a DELETE + INSERT on PK conflict, and the implicit DELETE ontask_detailsfails when child rows exist intask_historyorcache_outputs:This happens because
record_task_detailsis called multiple times with the same hash across different code paths (hashTask,hashTasks,hashBatchTasks), and by the second call, child rows already reference that hash.Expected Behavior
Use
INSERT ... ON CONFLICT DO UPDATE(upsert) which updates the existing row in-place without deleting it. No DELETE means no FK violation, while preserving the same idempotent behavior the code relies on.