1 Attachment(s)
QSqlTableModel Master/Detail example problem
running this code
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
Code:
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.
Re: QSqlTableModel Master/Detail example problem
Try calling details->submittAll() first.
Re: QSqlTableModel Master/Detail example problem
Quote:
Originally Posted by
norobro
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.