-
Notifications
You must be signed in to change notification settings - Fork 832
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
PRAGMA queries are not recognised #3237
Comments
#3239 will fix the queries getting combined. |
I am currently evaluating slqc for my project and stumbled over this question. Couldn't you enable foreign key checks via options to your sqlite connection string instead of pragmas? For example initializing your DB like this would enable foreign key support: db, err := sql.Connect("sqlite3", "file:test.db?_foreign_keys=on") Would that work with slqc or am I missing something? This assumes you are using the go-sqlite3 driver. For details can be found here: Connection String |
This is interesting. I'm using the 'modernc' sqlite implementation, which is considerably less documented when it comes to the connection string. I can see if those connection string options work with that too. |
Unfortunately, no, this does not work. This is the code I used to test: package main
import (
"database/sql"
"fmt"
_ "modernc.org/sqlite"
)
func main() {
db, err := sql.Open("sqlite", "file:flamenco-manager.sqlite?_foreign_keys=on")
if err != nil {
panic(err)
}
row := db.QueryRow("PRAGMA foreign_keys")
var enabled bool
if err := row.Scan(&enabled); err != nil {
panic(err)
}
fmt.Println("Enabled:", enabled)
} |
For modernc you want: db, err := sql.Connect("sqlite", "file:test.db?_pragma=foreign_keys(on)") |
Note that, even though this might work for this particular case, it's not a workaround for the general issue with |
Had the same issue and ended up with this implementation using mattn: func InitSqlite() (*sql.DB, error) {
extensions, err := readExtensions()
if err != nil {
return nil, fmt.Errorf("reading extensions: %s", err)
}
sql.Register("sqlite3_ext",
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
_, err := conn.Exec(`
PRAGMA busy_timeout = 10000;
PRAGMA journal_mode = WAL;
PRAGMA journal_size_limit = 200000000;
PRAGMA synchronous = NORMAL;
PRAGMA foreign_keys = ON;
PRAGMA temp_store = MEMORY;
PRAGMA cache_size = -16000;
\`, nil)
return err
},
Extensions: extensions,
})
conn, err := sql.Open("sqlite3_ext", dbPath)
if err != nil {
return nil, fmt.Errorf("opening sqlite db: %s", err)
}
return conn, nil
} Got it from the pocketbase library. Here's how that project set up PRAGMA using modernc. |
Version
1.25.0
What happened?
SQLite requires a
PRAGMA
query to enable foreign key constraints. Unfortunately, sqlc does not seem to recognisePRAGMA
as a valid query, and actually collects everything until the first query it does recognise. Let me illustrate. In thequeries.sql
below, there are three namedPRAGMA
queries followed by anINSERT
. Instead of generating four query functions, sqlc actually just creates one:As you can see, instead of using
-- name: ...
as query boundary, sqlc groups everything together until theINSERT
.Relevant log output
No response
Database schema
SQL queries
Configuration
No response
Playground URL
https://play.sqlc.dev/p/ce20411be4624a0295c2e51b8eb075a4e1069ac3d6951778cd6a54f032831a3c
What operating system are you using?
Windows
What database engines are you using?
SQLite
What type of code are you generating?
Go
The text was updated successfully, but these errors were encountered: