basic-memory icon indicating copy to clipboard operation
basic-memory copied to clipboard

[BUG] `remove` command doesn't actually remove a project

Open wtfzambo opened this issue 1 year ago • 4 comments

Bug Description

The remove command bm project remove project-name doesn't actually remove the project from memory.db.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Install version 0.14.3
  2. Create any project
  3. Try to remove it via either the above command or asking the LLM
  4. Run bm project list
  5. See that the project is still there

Expected Behavior

For the project to be removed from the database after running the command

Actual Behavior

The removed project doesn't get removed

Environment

  • OS: MacOS 15.6
  • Python version: 3.12.1
  • Basic Memory version: 0.14.3
  • Installation method: uv

Additional Context

Logs
❯ bm project remove my-project
2025-08-14 21:51:31.590 | INFO     | basic_memory.utils:setup_logging:167 - ENV: 'test' Log level: 'INFO' Logging to .basic-memory/basic-memory-api.log
2025-08-14 21:51:31.591 | INFO     | basic_memory.config:setup_basic_memory_logging:367 - Basic Memory 0.14.3 (Project: main)
2025-08-14 21:51:32.607 | DEBUG    | basic_memory.api.template_loader:__init__:235 - Initialized template loader with directory: /Users/wtfzambo/.local/share/uv/tools/basic-memory/lib/python3.12/site-packages/basic_memory/templates
2025-08-14 21:51:32.715 | DEBUG    | basic_memory.mcp.async_client:create_client:23 - Creating ASGI client for local Basic Memory API
2025-08-14 21:51:32.760 | INFO     | basic_memory.services.initialization:initialize_app:156 - Initializing app...
2025-08-14 21:51:32.761 | DEBUG    | basic_memory.db:_create_engine_and_session:81 - Creating engine for db_url: sqlite+aiosqlite:////Users/wtfzambo/.basic-memory/memory.db
2025-08-14 21:51:32.767 | INFO     | basic_memory.db:run_migrations:176 - Running database migrations...
2025-08-14 21:51:32.772 | INFO     | basic_memory.db:run_migrations:195 - Migrations completed successfully
2025-08-14 21:51:32.772 | INFO     | basic_memory.repository.search_repository:init_search_index:115 - Initializing search index
2025-08-14 21:51:32.773 | INFO     | basic_memory.services.initialization:initialize_database:30 - Database initialization completed
2025-08-14 21:51:32.773 | INFO     | basic_memory.services.initialization:reconcile_projects_with_config:47 - Reconciling projects from config with database...
2025-08-14 21:51:32.774 | INFO     | basic_memory.services.project_service:synchronize_projects:233 - Synchronizing projects between database and configuration
2025-08-14 21:51:32.783 | DEBUG    | basic_memory.repository.repository:find_all:157 - Finding all Project (skip=0, limit=None)
2025-08-14 21:51:32.784 | DEBUG    | basic_memory.repository.repository:find_all:170 - Found 4 Project records
2025-08-14 21:51:32.786 | INFO     | basic_memory.services.project_service:synchronize_projects:301 - Project synchronization complete
2025-08-14 21:51:32.786 | INFO     | basic_memory.mcp.project_session:initialize:34 - Initialized project session with default project: main
2025-08-14 21:51:32.786 | INFO     | basic_memory.mcp.project_session:refresh_from_config:81 - Refreshed project session from config, new default: main
2025-08-14 21:51:32.786 | INFO     | basic_memory.services.initialization:reconcile_projects_with_config:64 - Projects successfully reconciled between config and database
2025-08-14 21:51:32.786 | INFO     | basic_memory.services.initialization:initialize_app:163 - App initialization completed (migration running in background if needed)
2025-08-14 21:51:32.786 | INFO     | basic_memory.services.initialization:ensure_initialization:177 - Initialization completed successfully: result=None
2025-08-14 21:51:32.786 | INFO     | basic_memory.mcp.project_session:set_current_project:53 - Switched project context: main -> main
2025-08-14 21:51:32.787 | DEBUG    | basic_memory.mcp.tools.utils:call_delete:466 - Calling DELETE '/projects/my-project'
2025-08-14 21:51:32.796 | INFO     | basic_memory.services.project_service:remove_project:147 - Project 'my-project' removed from configuration and database
Project 'my-project' removed successfully
Note: The project files have not been deleted from disk.

Possible Solution

No clue

wtfzambo avatar Aug 14 '25 20:08 wtfzambo

Same, I get this error:


sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) FOREIGN KEY constraint failed
[SQL: DELETE FROM project WHERE project.id = ?]
[parameters: (11,)]
(Background on this error at: https://sqlalche.me/e/20/gkpj)
Error removing project: (sqlite3.IntegrityError) FOREIGN KEY constraint failed
[SQL: DELETE FROM project WHERE project.id = ?]

(Background on this error at: https://sqlalche.me/e/20/gkpj)

johnlindquist avatar Aug 16 '25 16:08 johnlindquist

@claude I cannot reproduce this issue. Are you able to reproduce it?

jope-bm avatar Aug 19 '25 22:08 jope-bm

Claude finished @jope-bm's task —— View job • claude/issue-254-20250819-2203 • Create PR ➔


Claude finished @jope-bm's task


I successfully investigated and fixed the bug where the remove command doesn't actually remove projects from memory.db.

Root Cause & Solution

The Problem: The FOREIGN KEY constraint failed error occurs because migration 647e7a75e2cd_project_constraint_fix.py recreated the project table but forgot to re-establish the foreign key constraint from entity.project_id to project.id.

Key Findings:

  1. Original Setup: Migration 5fe1ab1ccebe_add_projects_table.py created the FK but without CASCADE DELETE:

    batch_op.create_foreign_key("fk_entity_project_id", "project", ["project_id"], ["id"])
    
  2. Constraint Lost: Migration 647e7a75e2cd recreated the project table but didn't re-add the FK constraint

  3. SQLAlchemy Models: The models correctly define cascade relationships, but they only work when database-level FK constraints exist

The Solution:

I've created migration a1b2c3d4e5f6_fix_project_foreign_keys.py that:

  • Re-establishes the missing FK constraint
  • Adds CASCADE DELETE to properly handle project removal with related entities
  • Safely handles cases where the constraint might already exist

Files Created:

  • 📄 Migration: src/basic_memory/alembic/versions/a1b2c3d4e5f6_fix_project_foreign_keys.py
  • 🧪 Test: tests/services/test_project_removal_bug.py - reproduces the bug and verifies the fix
  • 🔍 Debug script: test_project_removal_bug.py - standalone reproduction script

Expected Behavior After Fix:

  • bm project remove project-name works correctly
  • Related entities, observations, and relations are cascade deleted
  • No more FOREIGN KEY constraint failed errors

The migration will automatically run when users upgrade. This properly addresses both the original reporter's issue and @johnlindquist's foreign key constraint error.

claude[bot] avatar Aug 19 '25 22:08 claude[bot]

Fixed by https://github.com/basicmachines-co/basic-memory/pull/258

jope-bm avatar Aug 20 '25 17:08 jope-bm