Skip to content

fix: enable SQLite foreign key enforcement in get_dialect() - #620

Open
Tusm11 wants to merge 1 commit into
MemoriLabs:mainfrom
Tusm11:fix/sqlite-foreign-keys-pragma
Open

fix: enable SQLite foreign key enforcement in get_dialect()#620
Tusm11 wants to merge 1 commit into
MemoriLabs:mainfrom
Tusm11:fix/sqlite-foreign-keys-pragma

Conversation

@Tusm11

@Tusm11 Tusm11 commented Aug 10, 2026

Copy link
Copy Markdown

Problem

SQLite disables foreign key constraint enforcement by default. This causes ON DELETE CASCADE constraints to be silently ignored, leaving orphaned rows in memori_entity_fact_mention table after delete_entity_memories() is called.

Example scenario:

  1. Entity has facts stored
  2. Mentions link facts to conversations
  3. User calls delete_entity_memories(entity_id)
  4. Facts get deleted -DONE
  5. But mention rows stay behind (should cascade delete)

Solution

Execute PRAGMA foreign_keys = ON when SQLite dialect is detected in the get_dialect() method. This enables proper foreign key constraint enforcement, making cascade deletes work as intended.

Changes Made

  • memori/storage/adapters/sqlalchemy/_adapter.py: Added PRAGMA statement for SQLite
  • tests/storage/drivers/test_sqlite_driver.py: Tests verify the behavior

Testing

  • All 26 SQLite driver tests pass
  • Ran uv run pytest tests/storage/drivers/test_sqlite_driver.py -v
  • No new issues or regressions

Impact

  • Python SDK (memori/): Affected
  • Storage adapters: Affected
  • Breaking changes: None - backward compatible
  • User-facing: Yes - fixes data integrity issue

@jayyao18

Copy link
Copy Markdown
Contributor

@Tusm11 we're working on exciting new features and will review this soon.

Join our Discord community to be the first to hear about the latest releases!

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