Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Make sure foreign_keys work as intended #764

Open
psarna opened this issue Oct 12, 2023 · 0 comments
Open

Make sure foreign_keys work as intended #764

psarna opened this issue Oct 12, 2023 · 0 comments

Comments

@psarna
Copy link
Contributor

psarna commented Oct 12, 2023

Foreign keys have a very specific implementation in libSQL/SQLite. In order for them to be effective, each db connection needs to activate PRAGMA foreign_keys=on, otherwise they are not honored. Moreover, as the official docs say:

It is not possible to enable or disable foreign key constraints in the middle of a multi-statement transaction (when SQLite is not in autocommit mode)

That's problematic with sqld's server model, because we do not generally guarantee that user sessions are always based on a single database connection, and the matter becomes even more problematic with replica write delegation.

If we don't do anything, users can observe anomalies, e.g. foreign key constraints or cascading deletes not working, despite them sending PRAGMA foreign_keys=on in the same database session.

We may need a mechanism that activates automatic PRAGMA foreign_keys=on server-side, e.g. for all connections coming from a particular user. Since we already parse statements client-side, we can even build the mechanism on top of PRAGMA foreign_keys=on, and send a specific request to the server if a user sends it. Or, we can make it even more coarse-grained and allow creating foreign-key-enabled databases that always start with foreign_keys=on -- under the assumption that foreign keys should generally either be honored at all times, or not at all.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant