soci: "foreign_keys" does not work (SQLite)

When opening a database with foreign keys enabled via the connection string, actually the feature is not enabled.

soci::session sql(soci::sqlite3, "db=/home/matheus/.local/share/argo/argo.db foreign_keys=true");
std::int32_t a = -1;
sql << "PRAGMA foreign_keys", soci::into(a);
std::cout << a << std::endl;

The output is 0.

When executing the query sql << "PRAGMA foreign_keys=true manually, the output is 1.

I already check the source code and the test and do not know why it does not work.

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 18 (18 by maintainers)

Most upvoted comments

4.0.3 doesn’t have foreign_keys, it is only on master

Your second test is exactly the same thing as I do above and I’ve tried adding = to the connection string and it didn’t change anything (which is arguably a bug, it should have resulted in an exception…), so I still can’t reproduce it and have no idea what could be going on here.

@Spixmaster Do you have a SSCCE?

I’ve written and run the following:

#include <soci/soci.h>

#include <iostream>

int main() {
    try {
        soci::session s;
        s.set_log_stream(&std::cout);
        s.open("sqlite3://db=:memory: foreign_keys=true");
        std::string fk;
        s << "pragma foreign_keys", soci::into(fk);
        std::cout << "FK = \"" << fk << "\"\n";
        return 0;
    } catch ( std::exception& e ) {
        std::cerr << "Exception: " << e.what() << "\n";
    } catch ( ... ) {
        std::cerr << "Unknown exception.\n";
    }

    return 127;
}

and it outputs

pragma foreign_keys
FK = "1"

as expected here.