fix: delete pages before versions to prevent foreign key constraint failure #253
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Users were unable to delete documentation versions through the web UI or API, encountering a
FOREIGN KEY constraint failederror. The only workaround was to delete the entire Docker volume, which was far from ideal.{"message":"Failed to remove docs mcp server: Failed to remove version caused by SqliteError: FOREIGN KEY constraint failed"}Root Cause
The database schema has a hierarchical structure with foreign key relationships:
Where:
pages.version_idREFERENCESversions(id)documents.page_idREFERENCESpages(id)When deleting a version, the code was executing deletions in the wrong order:
version_id)The
pagestable was introduced in migration 009 to normalize page-level metadata, but the deletion logic wasn't updated to account for this new intermediate table.Solution
Added a
deletePagesprepared statement and updated theremoveVersionmethod to delete entities in the correct order to respect foreign key constraints:documents.page_id → pages.id)pages.version_id → versions.id)versions.library_id → libraries.id)Changes
deletePagesprepared statement that deletes all pages for a given library and versionremoveVersionmethod to calldeletePagesbetween document and version deletionTesting
Impact
Users can now successfully delete documentation versions through the web UI and MCP tools without encountering foreign key constraint errors, eliminating the need to delete entire Docker volumes as a workaround.
Fixes #[issue_number]
Original prompt
Fixes #252
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.