Important: Please read the Qt Code of Conduct -

SQLite: Activating foreign key support not working

  • Hi all,

    how can I activate foreign key support from within QML/JS? I tried the following:

    function instance()
        var db = Storage.LocalStorage.openDatabaseSync("Db", "", "Nodes",
    5000000);  /* DB Size: 5MB */
        db.transaction(function(tx) {
            tx.executeSql("PRAGMA foreign_keys = ON;");
        return db
    function queryForeignKeysEnabled()
        instance().transaction(function(tx) {
            var rs = tx.executeSql("PRAGMA foreign_keys;");

    which returns: {"foreign_keys":0}

    Using the same statement in an interactive SQLite session works perfectly well.

    Thanks in advance,

    PS: In C++ [...] QSqlQuery query; query.exec("PRAGMA foreign_keys = ON;"); is reportedly working.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    AFAIK, QML implements WebSQL which doesn't provide support for PRAGMA.

    An alternative would be to use a custom model based on QSqlTableModel.

    Hope it helps.

  • Thanks for the reply and the warm welcome!

    Since I already went through MVC hell some time ago (it is undoubtedly very powerful) I will work around the problem. It's not that complicated but also not very elegant...


Log in to reply