Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreign key support in SQLite #167

Closed
foldu opened this issue Mar 26, 2020 · 2 comments
Closed

Foreign key support in SQLite #167

foldu opened this issue Mar 26, 2020 · 2 comments
Labels
db:sqlite Related to SQLite enhancement New feature or request good first issue Good for newcomers

Comments

@foldu
Copy link
Contributor

foldu commented Mar 26, 2020

If you want to use foreign keys in SQLite you need to enable them with PRAGMA FOREIGN KEYS = ON; on every new connection (see Section 2 of https://sqlite.org/foreignkeys.html).
It would be nice if sqlx's Pool had support for automatically doing this when it establishes a new connection.

@mehcode
Copy link
Member

mehcode commented Mar 28, 2020

@foldu Definitely agree the Pool needs something generic to handle doing stuff on lifecycle events.


Pool::builder()
  .on_connect(|conn: &mut C| { async move {
    // [...]
  }})
  .build()

I could imagine on_connect, on_acquire, on_release. Does this setup feel fine?


The other option is we could support a URL parameter for some pragmas like foreign_keys. E.g., sqlite://a.db?foreign_keys=on.

Thoughts? We definitely need the Pool hooks but this might be better for your specific case.

@mehcode mehcode added enhancement New feature or request good first issue Good for newcomers db:sqlite Related to SQLite labels Mar 28, 2020
@mehcode
Copy link
Member

mehcode commented Jul 5, 2020

This is now on by default (because yes) and there is a foreign_keys option in SqliteConnectOptions

@mehcode mehcode closed this as completed Jul 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
db:sqlite Related to SQLite enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants