mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 02:50:46 -06:00
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.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <QComboBox>
|
||||
#include <QDateTime>
|
||||
|
||||
#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;
|
||||
|
||||
Reference in New Issue
Block a user