Skip to content

[Database] Enable PRAGMA foreign_keys = ON for SQLite connections - #84

Open
LetMeSleep8h wants to merge 1 commit into
PicadoLabs:mainfrom
LetMeSleep8h:fix/sqlite-foreign-keys
Open

LetMeSleep8h wants to merge 1 commit into
PicadoLabs:mainfrom
LetMeSleep8h:fix/sqlite-foreign-keys

Conversation

@LetMeSleep8h

Copy link
Copy Markdown
Contributor

Fixes #80.

configure_sqlite_connection now also executes PRAGMA foreign_keys=ON and PRAGMA synchronous=NORMAL (the documented pairing with WAL).

  • The existing test_sqlite_connections_configure_journal_and_timeout now asserts both pragmas read back on every connection (foreign_keys=1, synchronous=1), in-memory and file-backed.
  • New test_sqlite_foreign_keys_enforce_cascade: creates parent/child tables with ON DELETE CASCADE, deletes the parent, and asserts the child row went with it — which is exactly what silently did not happen before.

pytest backend/tests/test_database_sqlite.py: 4 passed.

SQLite enforces foreign keys only when asked, per connection: without
this the cascade deletes between workspaces, keys and requests never
fire. Also sets synchronous=NORMAL, the documented pairing with WAL.
The pragma assertions join the existing connection test and a new
cascade test proves ON DELETE CASCADE now deletes.

Fixes PicadoLabs#80

@Kaap10 Kaap10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.

While the intent of enabling PRAGMA foreign_keys=ON and PRAGMA synchronous=NORMAL is correct, this change causes 21 test failures across test_redis_lifecycle.py and test_traffic_sse.py because child tables referencing requests.request_id do not have ondelete="CASCADE" configured on their foreign key columns in backend/app/storage/models.py:

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) FOREIGN KEY constraint failed [SQL: DELETE FROM requests]

Required Fixes:

  1. In backend/app/storage/models.py, add ondelete="CASCADE" to the foreign key columns referencing requests.request_id:
    • RoutingDecisionRecord: ForeignKey("requests.request_id", ondelete="CASCADE")
    • ResponseRecord: ForeignKey("requests.request_id", ondelete="CASCADE")
    • FeedbackRecord: ForeignKey("requests.request_id", ondelete="CASCADE")
    • ExperimentRunRecord: ForeignKey("requests.request_id", ondelete="CASCADE")
  2. In backend/tests/test_redis_lifecycle.py and backend/tests/test_traffic_sse.py, ensure test cleanup cleans child records or correctly exercises cascade deletes.
  3. Run the full test suite (pytest backend/tests) to verify all 144 tests pass before requesting re-review.

@Kaap10

Kaap10 commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

@LetMeSleep8h just checking in to see the progress. Let me know any blockers you are facing.

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.

[Database] Enable PRAGMA foreign_keys = ON for SQLite Connections

2 participants