Skip to content

fix: Add missing foreign key constraints for project removal (#254)#258

Merged
jope-bm merged 4 commits into
mainfrom
claude/issue-254-20250819-2203
Aug 20, 2025
Merged

fix: Add missing foreign key constraints for project removal (#254)#258
jope-bm merged 4 commits into
mainfrom
claude/issue-254-20250819-2203

Conversation

@jope-bm
Copy link
Copy Markdown
Contributor

@jope-bm jope-bm commented Aug 20, 2025

Summary

Fixes issue #254: Foreign key constraint failures when deleting projects with related entities.

  • Root Cause: Migration 647e7a75e2cd recreated the project table but failed to re-establish foreign key constraints with CASCADE DELETE
  • Impact: Users unable to delete projects containing entities, receiving FOREIGN KEY constraint failed errors
  • Solution: Add missing foreign key constraint with CASCADE DELETE behavior to ensure proper cleanup

Changes Made

Database Migration

  • Added migration a1b2c3d4e5f6_fix_project_foreign_keys.py to restore missing foreign key constraints
  • Changes entity.project_id foreign key from ON DELETE: NO ACTION to ON DELETE: CASCADE
  • Ensures automatic cleanup of related entities when projects are deleted

Comprehensive Database Tests

  • Added comprehensive test file tests/db/test_issue_254_foreign_key_constraints.py
  • Tests both the fix and reproduction scenarios to prevent regression
  • Validates foreign key constraint behavior in test environment

Production Verification Script

  • Added tests/test_production_cascade_delete.py for testing on actual production databases
  • Creates automatic backups before testing
  • Verifies schema configuration and functional cascade delete behavior
  • Essential for confirming fix works in production SQLite with foreign key enforcement

Technical Details

Before Fix:

-- Foreign key without cascade delete
FOREIGN KEY(project_id) REFERENCES project (id)  -- Defaults to NO ACTION

After Fix:

-- Foreign key with cascade delete
FOREIGN KEY(project_id) REFERENCES project (id) ON DELETE CASCADE

Why Tests Originally Passed:

  • Test environment uses in-memory SQLite without foreign key enforcement during schema creation
  • Production uses filesystem SQLite with PRAGMA foreign_keys=ON
  • This discrepancy masked the constraint issue until production verification script was created

Test Plan

  • Unit Tests: pytest tests/db/test_issue_254_foreign_key_constraints.py -v
  • Integration Tests: All existing repository and service tests pass
  • Production Verification: python tests/test_production_cascade_delete.py
    • Before fix: ❌ FOREIGN KEY constraint failed
    • After fix: ✅ CASCADE DELETE working: Entity was automatically deleted
  • Manual Testing: Verified project removal works correctly through CLI

Verification Results

Production Database Test Results:

🧪 TEST RESULTS:
  Schema has CASCADE DELETE: True
  CASCADE DELETE works: True  
✅ PASS: Foreign key constraints are properly configured with CASCADE DELETE

Key Validation Points:

  • Foreign key constraints properly configured with ON DELETE CASCADE
  • Project deletion succeeds without constraint errors
  • Related entities are automatically cleaned up
  • No data orphaning occurs

Breaking Changes

None. This is a pure bug fix that restores intended behavior.

Migration Safety

  • Migration uses SQLAlchemy's batch_alter_table for safe constraint modification
  • Handles cases where constraint may or may not already exist
  • Includes proper rollback mechanism in downgrade()

🤖 Generated with Claude Code

claude Bot and others added 4 commits August 19, 2025 20:05
- 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>
@jope-bm jope-bm force-pushed the claude/issue-254-20250819-2203 branch from a0a6b59 to 7330510 Compare August 20, 2025 02:05
@jope-bm jope-bm requested a review from phernandez August 20, 2025 02:37
Copy link
Copy Markdown
Member

@phernandez phernandez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, I wonder how I missed that.

@jope-bm jope-bm merged commit b6aeb32 into main Aug 20, 2025
9 checks passed
@jope-bm jope-bm deleted the claude/issue-254-20250819-2203 branch August 20, 2025 14:49
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