fix: flush session before upserting session_peers to prevent FK violation#720
fix: flush session before upserting session_peers to prevent FK violation#720TipKnuckle wants to merge 2 commits into
Conversation
…tion With autoflush=False in db.py, Core-level SQL statements like pg_insert do not automatically flush pending ORM objects. In get_or_create_session, db.add(honcho_session) marks the session as pending, but when _get_or_add_peers_to_session executes the pg_insert upsert on session_peers (which has a FK to sessions), the session row doesn't exist in PostgreSQL yet, causing a ForeignKeyViolation. Add await db.flush() after the session creation to ensure the session row exists before the FK-dependent upsert.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughSingle-line fix adding explicit ChangesSession creation ordering
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/crud/session.py`:
- Around line 229-233: The db.flush() call currently sits outside the savepoint
block and must be moved inside the async with db.begin_nested() context so any
IntegrityError during flush is confined to the nested transaction and the outer
transaction remains valid for the recursive retry; modify the flow in the
function where begin_nested() is used (the block that currently calls
_get_or_add_peers_to_session) to call await db.flush() while inside that async
with db.begin_nested():, then proceed to call _get_or_add_peers_to_session,
ensuring IntegrityError from flush triggers the nested rollback only and
preserves the retry behavior.
🪄 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: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 181f13dc-9a1e-40d1-ad29-2a670d2e3165
📒 Files selected for processing (1)
src/crud/session.py
Ensures any IntegrityError during flush is confined to the nested transaction so the outer transaction stays valid for the retry path.
|
Good catch — moved |
Description
With
autoflush=Falseinsrc/db.py, Core-level SQL statements likepg_insertdo not automatically flush pending ORM objects before executing. Inget_or_create_session,db.add(honcho_session)marks the session as pending, but when_get_or_add_peers_to_sessionexecutes thepg_insertupsert onsession_peers(which has a foreign key tosessions), the session row doesn't exist in PostgreSQL yet, causing aForeignKeyViolation.Fix
Added
await db.flush()after the session creation and before the peer upsert, ensuring the session row exists in the database before the FK-dependent upsert executes.Testing
Verified working with the OWUI sync script that was previously hitting this error (500 from server after a few imports).
Summary by CodeRabbit