Better messages for a table edition when foreign-key check detect problems

The message when there are problems and the user has made changes and
accepted them must be different to the case when the user has cancelled
and any possible changes have been already reverted by the dialog.

In the first case the fault may be ours and we actually revert changes.
In the second case, we know for sure the problems were there before
editing the table. We warn user about it, since we already know.

The last error from the DB is inappropriate for both cases because it
hasn't been updated by the foreign-key check.

Also fixed the typo in a function identifier.

Room for improvement: maybe the check should be done before opening the
dialog, so we don't let the user edit the table until the issues have
been solved.

See issue #1547
This commit is contained in:
mgr
2018-09-29 16:22:03 +02:00
parent f2fe2c921d
commit 071f963fcc
3 changed files with 14 additions and 8 deletions

View File

@@ -1045,10 +1045,16 @@ void MainWindow::editObject()
// If foreign_keys were enabled, we must commit or rollback the transaction so the foreign_keys pragma can be restored.
if (foreign_keys == "1") {
if (!db.querySingeValueFromDb(QString("PRAGMA %1.foreign_key_check").arg(sqlb::escapeIdentifier(name.schema()))).isNull()) {
QMessageBox::warning(this, QApplication::applicationName(),
tr("Error checking foreign keys after table modification. The changes will be reverted.\n"
"Message from database engine:\n%1").arg(db.lastError()));
if (!db.querySingleValueFromDb(QString("PRAGMA %1.foreign_key_check").arg(sqlb::escapeIdentifier(name.schema()))).isNull()) {
// Raise warning for accepted modification. When rejected, warn user also since we know now that the table has problems,
// but it wasn't our fault.
if (ok)
QMessageBox::warning(this, QApplication::applicationName(),
tr("Error checking foreign keys after table modification. The changes will be reverted."));
else
QMessageBox::warning(this, QApplication::applicationName(),
tr("This table did not pass a foreign-key check.<br/>"
"You should run 'Tools | Foreign-Key Check' and fix the reported issues."));
db.revertAll();
} else {
// Commit all changes so the foreign_keys can be effective.
@@ -1663,7 +1669,7 @@ void MainWindow::importDatabaseFromSQL()
QApplication::restoreOverrideCursor();
if(!ok)
QMessageBox::warning(this, QApplication::applicationName(), tr("Error importing data: %1").arg(db.lastError()));
else if(db.getPragma("foreign_keys") == "1" && !db.querySingeValueFromDb(QString("PRAGMA foreign_key_check")).isNull())
else if(db.getPragma("foreign_keys") == "1" && !db.querySingleValueFromDb(QString("PRAGMA foreign_key_check")).isNull())
QMessageBox::warning(this, QApplication::applicationName(), tr("Import completed. Some foreign key constraints are violated. Please fix them before saving."));
else
QMessageBox::information(this, QApplication::applicationName(), tr("Import completed."));