Skip to content

fix: enable foreign key constraints in SQLite connection#47

Open
echobt wants to merge 1 commit intomainfrom
fix/issue-138
Open

fix: enable foreign key constraints in SQLite connection#47
echobt wants to merge 1 commit intomainfrom
fix/issue-138

Conversation

@echobt
Copy link
Copy Markdown
Contributor

@echobt echobt commented Jan 20, 2026

Description

This PR fixes a bug where SQLite foreign key constraints were not being enforced, leading to orphaned chunks in the database when files were deleted or replaced.

SQLite does not enforce foreign keys by default, so they must be explicitly enabled for each connection. This change adds PRAGMA foreign_keys = ON to the database connection initialization.

Fix Details

  • Modified Database::new in src/core/db.rs to execute PRAGMA foreign_keys = ON immediately after opening the connection.

Verification

I created a reproduction script that simulates the issue:

  1. Insert a file.
  2. Insert a chunk linked to that file.
  3. Perform an INSERT OR REPLACE on the file (which deletes the old file row).
  4. Verify if the chunk remains (orphaned) or is deleted.

Before Fix:
The chunk remained orphaned because the cascade delete was not triggered.

After Fix:
The chunk is correctly deleted via the ON DELETE CASCADE constraint.

Also ran existing tests with cargo test and they passed.

This change enables foreign key enforcement for every new database connection by executing 'PRAGMA foreign_keys = ON'. This ensures that cascading deletes defined in the schema are respected, preventing orphaned chunks when files are deleted or replaced.
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.

1 participant