Repository navigation
Closed
Labels
bugSomething isn't workingSomething isn't working
Milestone
Description
Description
File synchronization is failing with "Multiple rows were found when one or none was required" error due to permalink prefix matching returning multiple results across different projects.
Root Cause
The database contains entities with similar permalinks across different projects:
projects/basic-bot/basic-bot-sprint-4-production-deployment-complete(project: basic-bot-kb)projects/basic-bot/basic-bot-sprint-4-production-deployment(project: bm)
When the system searches for entities using a LIKE query or prefix matching, both entries are returned, causing the "multiple rows" error.
Database Evidence
SELECT id, title, permalink, file_path, project_id FROM entity
WHERE permalink LIKE '%basic-bot-sprint-4-production-deployment%';Returns:
189|BasicBot Sprint 4 - Production Deployment Complete|projects/basic-bot/basic-bot-sprint-4-production-deployment-complete|projects/basic-bot/BasicBot Sprint 4 - Production Deployment Complete.md|7
190|BasicBot Sprint 4 Production Deployment|projects/basic-bot/basic-bot-sprint-4-production-deployment|basic-bot/BasicBot Sprint 4 - Production Deployment Complete.md|2
Error Details
2025-06-22 23:08:22.069 | ERROR | basic_memory.services.entity_service:create_entity_from_markdown:318 - Failed to upsert entity for projects/basic-bot/BasicBot Sprint 4 - Production Deployment Complete.md: Multiple rows were found when one or none was required
Impact
- File synchronization fails when files have similar names across projects
- The watch service cannot process file changes
- Users experience sync failures for legitimate files
Steps to Reproduce
- Have files with similar names in different projects that generate similar permalinks
- One permalink is a prefix of another (e.g., "foo" and "foo-complete")
- File sync attempts to find existing entity using prefix matching
- Multiple rows are returned, causing the error
Expected Behavior
- Queries should be scoped to the specific project
- Exact permalink matching should be used instead of prefix matching
- Or if prefix matching is needed, handle multiple results appropriately
Actual Behavior
The query returns multiple rows from different projects, causing the operation to fail.
Environment
- Basic Memory version: 0.13.7
- SQLite database with unique constraints on (file_path, project_id) and (permalink, project_id)
Suggested Fix
- Ensure all entity queries are properly scoped by project_id
- Use exact permalink matching instead of LIKE queries where appropriate
- If prefix matching is necessary, handle multiple results explicitly
- Add additional constraints to ensure queries return at most one row
SQL Schema Context
The entity table has these unique constraints:
UNIQUE INDEX uix_entity_file_path_project ON entity (file_path, project_id)UNIQUE INDEX uix_entity_permalink_project ON entity (permalink, project_id) WHERE content_type = 'text/markdown' AND permalink IS NOT NULL
The issue occurs when queries don't properly include the project_id constraint or use overly broad matching patterns.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity
groksrc commentedon Jun 23, 2025
basic-memory-mcp.log
groksrc commentedon Jun 23, 2025
@claude take a look at this and see if you can fix it.
claude commentedon Jun 23, 2025
Claude finished @groksrc's task —— View job •
claude/issue-167-20250623_043531• Create PR ➔Multiple Rows Error Fix - Complete ✅
Root Cause Analysis
The issue occurred when file synchronization tried to find existing entities using permalink matching, but queries returned multiple results across different projects instead of being properly scoped by
project_id.The problem was in
EntityRepository.upsert_entity()method where several queries usedselect(Entity)directly instead ofself.select(), bypassing the automatic project filtering implemented in the base Repository class.Todo List
Fix Applied
Replaced
select(Entity)withself.select()in all four problematic locations:This ensures all queries are properly scoped by
project_id, preventing the "multiple rows" error when similar permalinks exist across different projects.Testing
I was unable to run the test suite due to permission restrictions, but the fix is straightforward:
You can run the tests with:
uv run pytest -p pytest_mock -vorjust testChanges Committed
src/basic_memory/repository/entity_repository.pyd5ac5c67cab7a7e429d890d7c5c3540b69fa0b8btest: add multi-project scoping tests for upsert_entity method
phernandez commentedon Jun 23, 2025
@groksrc nice