fix: add ON DELETE CASCADE to span.trace_id foreign key#12734
fix: add ON DELETE CASCADE to span.trace_id foreign key#12734Jah-yee wants to merge 1 commit intolangflow-ai:mainfrom
Conversation
When deleting a flow, trace rows get deleted (flow_id -> trace ON DELETE CASCADE). But span.trace_id -> trace.id did NOT have ON DELETE CASCADE, causing FK violation when trace rows tried to delete. Now span.trace_id will cascade delete when parent trace is deleted. Fixes langflow-ai#12732
WalkthroughUpdated the Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes Important Pre-merge checks failedPlease resolve all errors before merging. Addressing warnings is optional. ❌ Failed checks (1 error, 1 warning, 1 inconclusive)
✅ Passed checks (6 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/backend/base/langflow/services/database/models/traces/model.py`:
- Line 254: Add an Alembic migration to update the existing PostgreSQL foreign
key for span.trace_id to include ON DELETE CASCADE: create a new revision that
in upgrade() drops the current FK constraint on the span.trace_id column and
recreates it with ON DELETE CASCADE, and in downgrade() reverses that by
dropping the CASCADE FK and recreating the original FK without ON DELETE; follow
the same pattern and naming conventions used in migration
0e6138e7a0c2_add_ondelete_cascade_to_file_user_id_fk.py and target the
span.trace_id constraint referenced by the model change (trace_id: UUID Field
with foreign_key="trace.id", ondelete="CASCADE").
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: def9b561-f78e-4b2d-a52b-093279648d35
📒 Files selected for processing (1)
src/backend/base/langflow/services/database/models/traces/model.py
|
|
||
| id: UUID = Field(default_factory=uuid4, primary_key=True) | ||
| trace_id: UUID = Field(foreign_key="trace.id", index=True, description="Parent trace ID") | ||
| trace_id: UUID = Field(foreign_key="trace.id", ondelete="CASCADE", index=True, description="Parent trace ID") |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# 1) Find migrations that touch the span->trace FK behavior
rg -n -C3 'span_trace_id_fkey|span\.trace_id|trace\.id|ON DELETE CASCADE|on delete cascade|create_foreign_key|drop_constraint' \
src/backend/base/langflow/alembic/versions
# 2) List migration files for manual inspection if needed
fd . src/backend/base/langflow/alembic/versionsRepository: langflow-ai/langflow
Length of output: 30080
🏁 Script executed:
# 1. Check the exact model change at line 254
sed -n '250,260p' src/backend/base/langflow/services/database/models/traces/model.py
# 2. Search more specifically for span.trace_id FK migrations
rg -l 'span' src/backend/base/langflow/alembic/versions/ | xargs grep -l 'trace_id.*ondelete\|ondelete.*trace_id\|span.*trace.*CASCADE'
# 3. Check if there are any migrations specifically mentioning "span" table updates for trace_id
fd . src/backend/base/langflow/alembic/versions/ --exec grep -l 'batch_alter_table.*span' {} \;Repository: langflow-ai/langflow
Length of output: 528
🏁 Script executed:
# Check the initial span table creation migration to see if CASCADE was already there
cat src/backend/base/langflow/alembic/versions/3478f0bd6ccb_add_trace_and_span_tables.py | head -100
# Also check if there are any subsequent migrations that alter the span table
fd . src/backend/base/langflow/alembic/versions/ --exec grep -l 'batch_alter_table.*"span"' {} \;Repository: langflow-ai/langflow
Length of output: 4783
🏁 Script executed:
# Check all migrations that alter the span table
cd src/backend/base/langflow/alembic/versions && for file in *.py; do
if grep -q 'batch_alter_table.*"span"' "$file"; then
echo "=== $file ==="
grep -A 10 'batch_alter_table.*"span"' "$file" | head -20
fi
doneRepository: langflow-ai/langflow
Length of output: 1080
Add an Alembic migration for the span.trace_id FK CASCADE change
The model change at line 254 adds ondelete="CASCADE" to the foreign key definition, but this only affects new database instances. Existing PostgreSQL databases will retain the original constraint without CASCADE until an explicit Alembic migration updates it.
Create a migration that drops and recreates the span.trace_id foreign key constraint with ON DELETE CASCADE, following the pattern used in migration 0e6138e7a0c2_add_ondelete_cascade_to_file_user_id_fk.py.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/backend/base/langflow/services/database/models/traces/model.py` at line
254, Add an Alembic migration to update the existing PostgreSQL foreign key for
span.trace_id to include ON DELETE CASCADE: create a new revision that in
upgrade() drops the current FK constraint on the span.trace_id column and
recreates it with ON DELETE CASCADE, and in downgrade() reverses that by
dropping the CASCADE FK and recreating the original FK without ON DELETE; follow
the same pattern and naming conventions used in migration
0e6138e7a0c2_add_ondelete_cascade_to_file_user_id_fk.py and target the
span.trace_id constraint referenced by the model change (trace_id: UUID Field
with foreign_key="trace.id", ondelete="CASCADE").
Summary
When deleting a flow, trace rows get deleted (flow_id -> trace ON DELETE CASCADE).
But span.trace_id -> trace.id did NOT have ON DELETE CASCADE,
causing FK violation when trace rows tried to delete:
Fix
Added
ondelete="CASCADE"toSpanTable.trace_idfield:Testing
Closes #12732
Summary by CodeRabbit