Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Foreign key is ignored when deleting
Forum Updated to NodeBB v4.3 + New Features

Foreign key is ignored when deleting

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AsimovA Offline
    AsimovA Offline
    Asimov
    wrote on last edited by Asimov
    #1

    I have two tables referentially linked by a foreign key. The tables are in a sqlite database.

    query.exec("PRAGMA foreign_keys = ON;");
            query.exec("create table boxes "
                      "(boxID integer primary key, "
                      "boxName varchar(20), "
                      "boxLocation varchar(30))");
    
    query.exec("create table boxContents "
                      "(contentsID integer primary key,"
                       "boxID integer,"
                      "itemName varchar(20),"
                      "itemDescription varchar(30),"
                       "FOREIGN KEY(boxID) REFERENCES boxes(boxID) ON DELETE CASCADE)");
    

    I am used to doing this kind of thing in php and mysql. Normally when you add a foreign key on delete cascade and I delete the item from the first table, it also removes the items from the second table.
    In my first table I am displaying it using a model, and I delete with the following code.

     int deleteMe = getRecordID(ui,modelBox,"box");// Gets the id to delete
        modelBox->removeRow(deleteMe);
    

    The delete is successful, but it is not deleting the referentially linked table.
    This does not seem right as delete on cascade should mean that as soon as I delete from the main table it should deleted the linked files in the other table.

    Why is referential integrity being ignored?

    1 Reply Last reply
    0
    • AsimovA Offline
      AsimovA Offline
      Asimov
      wrote on last edited by
      #2

      I have found the solution.
      I thought it was enough to turn on foreign keys when creating the tables eg

      query.exec("PRAGMA foreign_keys = ON;");
      

      That is not the case you have to turn on foreign keys when you first connect to the database, as in the following code.
      This has solved my problem, and now when I delete in the one table, it deletes the data in the other table.

      The only problem I have to work out now is how to remove the blank line after deleting. So I have to research refreshing a model after deleting.

      bool DbManager::connOpen()
      {
          mydb = QSqlDatabase::addDatabase("QSQLITE");
          mydb.setDatabaseName(myConnection);
      
          if (!mydb.open())
          {
              qDebug() << "Error: connection with database fail";
          }
          else
          {
              qDebug() << "Database: connection ok";
              QSqlQuery query;
              query.exec("PRAGMA foreign_keys = ON;");
          }
      }
      
      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        What blank line ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved