From a69c62790f67dccf931544cc9813167387d7e964 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Sun, 22 Sep 2019 22:06:04 +0200 Subject: [PATCH] Fix foreign key editor not working correctly in Edit Table dialog When creating a new foreign key constraint in the Edit Table dialog, a list of tables to reference is displayed. This list is updated whenever the name of the edited table changes because this is the only table the name of which can change while the Edit Table dialog is opened. However, this meant that when the name of the table changes to some existing table name, the field list of that existing table is replaced by the field list of the current table. If the name of the current table is changed once again, it is deleted entirely from the list. This commit fixes this behaviour by separating the static part of the table and field list from the one table which can change. See issue #1991. --- src/EditTableDialog.cpp | 1 - src/ForeignKeyEditorDelegate.cpp | 26 +++++++++++++++----------- src/ForeignKeyEditorDelegate.h | 2 -- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/EditTableDialog.cpp b/src/EditTableDialog.cpp index 9cc3d882..6a3a2dea 100644 --- a/src/EditTableDialog.cpp +++ b/src/EditTableDialog.cpp @@ -374,7 +374,6 @@ void EditTableDialog::checkInput() if (normTableName != m_table.name()) { const std::string oldTableName = m_table.name(); m_table.setName(normTableName); - m_fkEditorDelegate->updateTablesList(oldTableName); // update fk's that refer to table itself recursively const auto& fields = m_table.fields; diff --git a/src/ForeignKeyEditorDelegate.cpp b/src/ForeignKeyEditorDelegate.cpp index 971dcd4a..b21c3f84 100644 --- a/src/ForeignKeyEditorDelegate.cpp +++ b/src/ForeignKeyEditorDelegate.cpp @@ -85,7 +85,8 @@ ForeignKeyEditorDelegate::ForeignKeyEditorDelegate(const DBBrowserDB& db, sqlb:: { for(const auto& jt : it.second) { - if(jt.second->type() == sqlb::Object::Types::Table) + // Don't insert the current table into the list. The name and fields of the current table are always taken from the m_table reference + if(jt.second->type() == sqlb::Object::Types::Table && jt.second->name() != m_table.name()) m_tablesIds.insert({jt.second->name(), std::dynamic_pointer_cast(jt.second)->fieldNames()}); } } @@ -105,14 +106,25 @@ QWidget* ForeignKeyEditorDelegate::createEditor(QWidget* parent, const QStyleOpt QComboBox* box = editor->idsComboBox; box->clear(); box->addItem(QString()); // for those heroes who don't like to specify key explicitly - for(const auto& n : m_tablesIds[tableName.toStdString()]) - box->addItem(QString::fromStdString(n)); + + // For recursive foreign keys get the field list from the m_table reference. For other foreign keys from the prepared field lists. + if(tableName.toStdString() == m_table.name()) + { + for(const auto& n : m_table.fieldNames()) + box->addItem(QString::fromStdString(n)); + } else { + for(const auto& n : m_tablesIds[tableName.toStdString()]) + box->addItem(QString::fromStdString(n)); + } + + box->setCurrentIndex(0); }); editor->tablesComboBox->clear(); for(const auto& i : m_tablesIds) editor->tablesComboBox->addItem(QString::fromStdString(i.first)); + editor->tablesComboBox->addItem(QString::fromStdString(m_table.name())); // For recursive foreign keys return editor; } @@ -175,12 +187,4 @@ void ForeignKeyEditorDelegate::updateEditorGeometry(QWidget* editor, const QStyl editor->setGeometry(option.rect); } -void ForeignKeyEditorDelegate::updateTablesList(const std::string& oldTableName) -{ - // this is used for recursive table constraints when - // table column references column within same table - m_tablesIds.erase(oldTableName); - m_tablesIds.insert({m_table.name(), m_table.fieldNames()}); -} - #include "ForeignKeyEditorDelegate.moc" diff --git a/src/ForeignKeyEditorDelegate.h b/src/ForeignKeyEditorDelegate.h index 8f6560a6..076706a9 100644 --- a/src/ForeignKeyEditorDelegate.h +++ b/src/ForeignKeyEditorDelegate.h @@ -25,8 +25,6 @@ public: void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override; - void updateTablesList(const std::string& oldTableName); - private: const DBBrowserDB& m_db; sqlb::Table& m_table;