Skip to content

fix: wrap pfp upload and user update in transaction to prevent foreign key errors#6

Merged
FrenchToblerone54 merged 2 commits intoParley-Chat:mainfrom
TerryCodes:fix-pfp-foreign-key-error
Feb 22, 2026
Merged

fix: wrap pfp upload and user update in transaction to prevent foreign key errors#6
FrenchToblerone54 merged 2 commits intoParley-Chat:mainfrom
TerryCodes:fix-pfp-foreign-key-error

Conversation

@TerryCodes
Copy link
Copy Markdown
Contributor

@TerryCodes TerryCodes commented Feb 22, 2026

This PR fixes two issues:

  1. FOREIGN KEY constraint error when updating user profile pictures

The issue was caused by the function creating its own database connection that was closed before the user update. This led to a race condition where the file record might not be visible when trying to update the user table.

Fix:

  • Modified to accept an optional database connection parameter
  • Refactored to use a transaction that wraps both the PFP processing and user update
  • This ensures atomicity - both the file insert and user update either succeed or fail together
  • Moved the cleanup of old PFP files outside the transaction to avoid interfering with the atomic operation
  1. Files not uploaded after adding file.content_length check

The issue was that when is None (which can happen for some file uploads), the condition failed and files were rejected.

Fix:

  • Changed the condition to make content_length check optional:
  • If content_length is available, use it for early rejection
  • If content_length is None, fall back to the chunked file size check
  • This ensures files are still uploaded even when content_length is not provided

@FrenchToblerone54 FrenchToblerone54 merged commit 1e70eae into Parley-Chat:main Feb 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants