SQLite Forum

I can't get the foreign_keys pragma to "stick"
Login
Hello,

As part of a unit test apparatus, I have created an SQL file that loads into sqlite3 and creates a bunch of tables for me. In that sql file, I attempt to enforce foreign_keys with the pragma:

```
PRAGMA foreign_keys = on; -- also 1, true
```

The pragma seems to work when I load the sql file directly:

```
$ sqlite3 -init sqlite3-empty.sql
-- Loading resources from sqlite3-empty.sql
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> pragma foreign_keys;
1
sqlite> .quit
```

But the pragma is not set correctly in the resulting DB file. This is loaded in a golang program (more detail [here](https://stackoverflow.com/questions/64597611/sqlite3-in-golang-foreign-keys-pragma-doesnt-enforce-keys ) but the same effect can be seen at the CLI:

```
$ sqlite3 -init sqlite3-empty.sql
-- Loading resources from sqlite3-empty.sql
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> pragma foreign_keys;
1
sqlite> .backup backup.db
sqlite> .quit
$ sqlite3 backup.db
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> pragma foreign_keys;
foreign_keys
------------
0
sqlite>
```

I can get it to "stick" if I set the pragma in my ~/.sqliterc file but this is a non-starter as this rig has to work in automated testing. 

Can I set the pragma via an environment variable or something? Any suggestions?

Thanks in advance!