fix: add ON DELETE CASCADE to span.trace_id foreign key#12384
fix: add ON DELETE CASCADE to span.trace_id foreign key#12384xr843 wants to merge 4 commits intolangflow-ai:mainfrom
Conversation
Flow deletion fails with ForeignKeyViolation when traces exist because the span.trace_id FK was created without ON DELETE CASCADE, breaking the cascade chain flow -> trace -> span. This commit: - Adds an Alembic migration to drop and re-create the span.trace_id FK with ON DELETE CASCADE (database-level fix) - Updates the SpanTable model to include ondelete="CASCADE" on trace_id - Adds explicit delete of SpanTable and TraceTable in cascade_delete_flow for defence in depth (matches existing pattern for other child tables) Fixes langflow-ai#12346 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
WalkthroughThis pull request fixes a foreign key constraint violation that occurs when deleting flows with associated execution traces. The fix applies both database-level changes (adding Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Important Pre-merge checks failedPlease resolve all errors before merging. Addressing warnings is optional. ❌ Failed checks (1 error, 4 warnings)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
CI Failure AnalysisAll three CI failures are unrelated to this PR's changes: 1. Ruff Style Check (PERF403) —
|
|
Hi maintainers, the Ruff Style Check failure is not related to this PR. The error is a pre-existing Happy to rebase once this is resolved upstream! |
Summary
span.trace_idFK withON DELETE CASCADE, completing the cascade chainflow → trace → spanat the database levelSpanTablemodel to includeondelete="CASCADE"ontrace_idfield so new deployments get the correct schemaDELETEforSpanTableandTraceTableincascade_delete_flow()for defence in depth, matching the existing pattern forMessageTable,TransactionTable,VertexBuildTable, andFlowVersionFixes #12346
Details
The original migration
3478f0bd6ccbcreated thespan.trace_idFK withoutondelete="CASCADE". When PostgreSQL cascades a flow deletion throughflow → trace(which correctly has CASCADE), it then tries to deletetracerows but fails becausespanrows still reference them without a cascade rule.Changes
alembic/versions/83cd04b12a34_add_ondelete_cascade_to_span_trace_id_fk.pyON DELETE CASCADEservices/database/models/traces/model.pyondelete="CASCADE"toSpanTable.trace_idFieldapi/utils/core.pycascade_delete_flow()Migration safety
batch_alter_tablefor SQLite compatibilitytable_existscheckdowngrade()function0e6138e7a0c2,59a272d6669a)Test plan
alembic upgrade headalembic upgrade headForeignKeyViolationalembic downgrade -1to verify reversibility🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Chores