Skip to content

Multiple rows error: Permalink prefix matching causes ambiguous results #167

Closed

Description

@groksrc

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

  1. Have files with similar names in different projects that generate similar permalinks
  2. One permalink is a prefix of another (e.g., "foo" and "foo-complete")
  3. File sync attempts to find existing entity using prefix matching
  4. 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

  1. Ensure all entity queries are properly scoped by project_id
  2. Use exact permalink matching instead of LIKE queries where appropriate
  3. If prefix matching is necessary, handle multiple results explicitly
  4. 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.

Activity

  1. groksrc commented on Jun 23, 2025

    @groksrc
    MemberAuthor
  2. groksrc commented on Jun 23, 2025

    @groksrc
    MemberAuthor

    @claude take a look at this and see if you can fix it.

  3. claude commented on Jun 23, 2025

    @claude
    Contributor

    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 used select(Entity) directly instead of self.select(), bypassing the automatic project filtering implemented in the base Repository class.

    Todo List

    • Examine the codebase structure and locate entity_service.py
    • Find the problematic query at line 318 that's causing the "multiple rows" error
    • Analyze the entity repository's upsert_entity method to identify LIKE queries
    • Analyze the database queries and identify where project_id scoping is missing
    • Implement fix to properly scope queries by project_id
    • Run tests to verify the fix works (unable to run due to permissions)
    • Commit the changes

    Fix Applied

    Replaced select(Entity) with self.select() in all four problematic locations:

    • ✅ Lines 144-148: Query after entity update
    • ✅ Lines 164-168: Query after entity insert
    • ✅ Lines 205-210: Query after race condition update
    • ✅ Lines 245-249: Query after permalink conflict resolution

    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:

    • Replaces direct SQL queries with project-scoped queries
    • Uses existing, well-tested project filtering mechanism from base Repository class
    • No behavioral changes, only proper scoping

    You can run the tests with: uv run pytest -p pytest_mock -v or just test

    Changes Committed

    • File: src/basic_memory/repository/entity_repository.py
    • Commit: d5ac5c67cab7a7e429d890d7c5c3540b69fa0b8b

  4. phernandez commented on Jun 23, 2025

    @phernandez
    Member
  5. added this to the v0.14.0 milestone on Jun 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions