EditTableDialog: Don't allow two columns with the same name

Don't allow the user to type in a field name that is already used in
the edited table. Show a warning and reset the name if he tries do so
anyway.

Also fix the warning for the foreign key check. Here a warning was shown
and the renaming cancelled but the field name wasn't reset to the old
value.
This commit is contained in:
Martin Kleusberg
2015-12-24 13:48:35 +01:00
parent 5af384f0a2
commit b04d52b99c

View File

@@ -216,6 +216,18 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
switch(column)
{
case kName:
// When a field of that name already exists, show a warning to the user and don't apply the new name
if(fieldNameExists(item->text(column)))
{
QMessageBox::warning(this, qApp->applicationName(), tr("There already is a field with that name. Please rename it first or choose a different "
"name for this field."));
// Reset the name to the old value but avoid calling this method again for that automatic change
ui->treeWidget->blockSignals(true);
item->setText(column, oldFieldName);
ui->treeWidget->blockSignals(false);
return;
}
// When editing an exiting table, check if any foreign keys would cause trouble in case this name is edited
if(!m_bNewTable)
{
@@ -229,6 +241,10 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
{
QMessageBox::warning(this, qApp->applicationName(), tr("This column is referenced in a foreign key in table %1, column %2 and thus "
"its name cannot be changed.").arg(fkobj.getname()).arg(fkfield->name()));
// Reset the name to the old value but avoid calling this method again for that automatic change
ui->treeWidget->blockSignals(true);
item->setText(column, oldFieldName);
ui->treeWidget->blockSignals(false);
return;
}
}