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. QSqlRelationalTableModel::removerow() deletes all the rows in the entire table if table doesn't have any explicit primary key defined

QSqlRelationalTableModel::removerow() deletes all the rows in the entire table if table doesn't have any explicit primary key defined

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 455 Views
  • 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.
  • D Offline
    D Offline
    divaindie
    wrote on last edited by
    #1

    Hi All,
    yesterday i have faced one strange behaviour in "QSqlRelationalTableModel" class which is if i call removeRow(row_no) function, it deletes all the rows in the entire table.
    for example : assume that sqlite db has table called "sample".whenever i click 'remove row' button in qml, below code is getting executed.

    //Here "QLSqlTimeTableModel" is derived class of "QSqlRelationalTableModel" class
    //input -->(2,"sample") i.e delete second row in table "sample"
    bool QLSqlTimeTableModel::remove(int index,QString TableName)
    {
        this->setTable(TableName);
        this->setEditStrategy(QSqlTableModel::OnManualSubmit);
        bool k;
        k = this->select();
        if(!k)
        {
         qDebug() << "QLSqlTimeTableModel:remove select statement failed !!";
            return k;
       }
        k =  this->removeRow(index);
        if( k == false) {
            qDebug() << "QLSqlTimeTableModel:remove failed " <<this->lastError();
             return k;
        }
        k =  this->submitAll();
        if(!k) {
         qDebug() << "QLSqlTimeTableModel::remove : submitAll failed "<< this->lastError();
        return k;
        }
        return k;
    }
    

    Also i have observed that if my table is created with explicit primary key defined then removeRow(row_no) works fine by deleting the given 'row_no' . but if my table doesn't have any explicit primary key defined then removeRow(row_no) deletes all the rows in the entire table.

    sqlite command which i used to create table with explicit primary key
    CREATE TABLE sample(id INTEGER PRIMARY KEY,name VARCHAR(10),address VARCHAR(50))
    command used to create table without primary key
    CREATE TABLE sample(name VARCHAR(10),address VARCHAR(50))

    But here my requirement is my table should not contain explicit primary key so that i do not have to specify the 'id' value each time i write into 'sample' table.

    How can i solve this problem ?

    thanks and regards
    divaIndie

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @divaindie said in QSqlRelationalTableModel::removerow() deletes all the rows in the entire table if table doesn't have any explicit primary key defined:

      How can i solve this problem ?

      Add a primary key or use a custom model. A remove can only happen correctly when there is a primary key (how it should work otherwise)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • D Offline
        D Offline
        divaindie
        wrote on last edited by
        #3

        thanks for reply!!.
        any other way to delete row in 'QSqlRelationalTableModel' other than calling removerow() function?

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          How do you think such a sql statement can look like when there is no primary key?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          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