Enable foreign keys

This commit is contained in:
2024-12-31 00:06:19 +01:00
parent e5e85d494e
commit 3c4cfe8e99
2 changed files with 2 additions and 8 deletions

View File

@ -8,7 +8,6 @@ import (
"context"
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log"
"log/slog"
"net/http"
"os"
@ -66,14 +65,11 @@ func main() {
}
func openDb() (*sql.DB, error) {
db, err := sql.Open("sqlite3", "./qv.sqlite")
db, err := sql.Open("sqlite3", "./qv.sqlite?_foreign_keys=on")
if err == nil {
err = db.Ping()
}
_, err = db.Exec("PRAGMA foreign_keys = ON")
if err != nil {
log.Fatalf("Failed to enable foreign key constraints: %v", err)
}
return db, err
}

View File

@ -70,5 +70,3 @@ CREATE TRIGGER prevent_created_at_update_votes
BEGIN
SELECT RAISE(FAIL, 'created_at column is read-only');
END;
PRAGMA foreign_keys = ON; -- run after opening the connection so foreign key constraints are checked