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;