QSqlRelationalTableModel::removerow() deletes all the rows in the entire table if table doesn't have any explicit primary key defined
-
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 -
@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)
-
How do you think such a sql statement can look like when there is no primary key?