From 26faa9c0fbf206f6f9c8c2132f2b3ed50a807428 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Sun, 2 Jun 2013 21:50:25 +0200 Subject: [PATCH] EditTableDialog: Check before setting NN when editing existing table When setting the Not Null flag of a column in an existing table check if there are rows with NULL values in this column before making any changes. This avoids an error in renameColumn() caused by the INSERT INTO ... SELECT ... statement failing. In a next step we could check if a default value for this column has been specified and use this in these kind of situations. --- src/EditTableDialog.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/EditTableDialog.cpp b/src/EditTableDialog.cpp index e8b57d66..165d5920 100644 --- a/src/EditTableDialog.cpp +++ b/src/EditTableDialog.cpp @@ -6,6 +6,7 @@ #include #include +#include "sqlitetablemodel.h" #include "sqlitedb.h" EditTableDialog::EditTableDialog(DBBrowserDB* db, const QString& tableName, QWidget* parent) @@ -229,6 +230,23 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column) break; case kNotNull: { + // When editing an existing table and trying to set a column to Not Null an extra check is needed + if(!m_bNewTable && item->checkState(column) == Qt::Checked) + { + // Because our renameColumn() function fails when setting a column to Not Null when it already contains some NULL values + // we need to check for this case and cancel here. Maybe we can think of some way to modify the INSERT INTO ... SELECT statement + // to at least replace all troublesome NULL values by the default value + SqliteTableModel m(this, pdb); + m.setQuery(QString("SELECT COUNT(rowid) FROM `%1` WHERE `%2` IS NULL;").arg(curTable).arg(field->name())); + if(m.data(m.index(0, 0)).toInt() > 0) + { + // There is a NULL value, so print an error message, uncheck the combobox, and return here + QMessageBox::information(this, qApp->applicationName(), tr("There is at least one row with this field set to NULL. " + "This makes it impossible to set this flag. Please change the table data first.")); + item->setCheckState(column, Qt::Unchecked); + return; + } + } field->setNotNull(item->checkState(column) == Qt::Checked); if(!m_bNewTable) callRenameColumn = true;