mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 11:00:44 -06:00
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.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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<sqlb::Table>(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"
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user