fix: Add missing foreign key constraints for project removal (#254)#258
Merged
Conversation
- Creates migration a1b2c3d4e5f6 to re-establish FK constraint from entity.project_id to project.id - Adds CASCADE DELETE to properly handle project removal with related entities - Fixes FOREIGN KEY constraint failed error when removing projects with entities, observations, or relations - Includes comprehensive test to reproduce and verify the fix Resolves #254 Co-authored-by: jope-bm <jope-bm@users.noreply.github.com> Signed-off-by: Joe P <joe@basicmemory.com>
Add comprehensive test coverage for database operations during project removal, including foreign key constraint validation and cascade behavior testing. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: joe@basicmemory.com Signed-off-by: Joe P <joe@basicmemory.com>
Add comprehensive test script to verify CASCADE DELETE behavior on production SQLite database at ~/.basic-memory/memory.db. The script: - Creates automatic backup before testing - Tests foreign key constraint configuration - Verifies CASCADE DELETE works correctly - Restores from backup after completion This script was essential for confirming issue #254 and verifying the fix works correctly in production environments. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Joe P <joe@basicmemory.com>
Remove tests/test_project_removal_bug.py as it's a duplicate of the existing test_issue_254_foreign_key_constraints.py test. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Joe P <joe@basicmemory.com>
a0a6b59 to
7330510
Compare
phernandez
approved these changes
Aug 20, 2025
Member
phernandez
left a comment
There was a problem hiding this comment.
huh, I wonder how I missed that.
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes issue #254: Foreign key constraint failures when deleting projects with related entities.
647e7a75e2cdrecreated the project table but failed to re-establish foreign key constraints withCASCADE DELETEFOREIGN KEY constraint failederrorsCASCADE DELETEbehavior to ensure proper cleanupChanges Made
Database Migration
a1b2c3d4e5f6_fix_project_foreign_keys.pyto restore missing foreign key constraintsentity.project_idforeign key fromON DELETE: NO ACTIONtoON DELETE: CASCADEComprehensive Database Tests
tests/db/test_issue_254_foreign_key_constraints.pyProduction Verification Script
tests/test_production_cascade_delete.pyfor testing on actual production databasesTechnical Details
Before Fix:
After Fix:
Why Tests Originally Passed:
PRAGMA foreign_keys=ONTest Plan
pytest tests/db/test_issue_254_foreign_key_constraints.py -vpython tests/test_production_cascade_delete.pyFOREIGN KEY constraint failedCASCADE DELETE working: Entity was automatically deletedVerification Results
Production Database Test Results:
Key Validation Points:
ON DELETE CASCADEBreaking Changes
None. This is a pure bug fix that restores intended behavior.
Migration Safety
batch_alter_tablefor safe constraint modificationdowngrade()🤖 Generated with Claude Code