PDA

View Full Version : QSqlTableModel Master/Detail example problem



MementoMori
29th April 2011, 18:15
running this code



MasterDialog dlg(this->master, this->details, index, this);
if(dlg.exec() == QDialog::Accepted){

dlg.submit();

if(!this->master->submitAll()){
QMessageBox::critical(this, "cant save data", master->lastError().text());
this->master->revertAll();
this->details->revertAll();
return;
}

if(!this->details->submitAll()){
QMessageBox::critical(this, "cant save data", details->lastError().text());
this->master->revertAll();
this->details->revertAll();
return;
}


}


changes from the details model aren't saved to the database. If you comment



if(!this->master->submitAll()){
QMessageBox::critical(this, "cant save data", master->lastError().text());
this->master->revertAll();
this->details->revertAll();
return;
}



the details are correctly saved.
the project is attached. It uses a sqlite memory db so it should be simple to run and test it.

norobro
30th April 2011, 02:30
Try calling details->submittAll() first.

MementoMori
30th April 2011, 14:54
Try calling details->submittAll() first.

Even if it worked in this case it's an error because if details contains fields (foreign key values) referring to master records which aren't in the database you'll get a foreign key violation error. This will surely happen every time you add a new master and its details at the same time.
so you always have to commit master changes before committing details ones.