diff --git a/sources/dataBase/projectdatabase.cpp b/sources/dataBase/projectdatabase.cpp index 75bff0ed6..23bd0e94e 100644 --- a/sources/dataBase/projectdatabase.cpp +++ b/sources/dataBase/projectdatabase.cpp @@ -319,10 +319,23 @@ void projectDataBase::addElement(Element *element) void projectDataBase::removeElement(Element *element) { m_content_changed = true; + bool changed = false; + m_remove_element_query.bindValue(":uuid", element->uuid().toString()); - if(!m_remove_element_query.exec()) { - qDebug() << "projectDataBase::removeElement remove error : " << m_remove_element_query.lastError(); + if (m_remove_element_query.exec()) { + changed = true; } else { + qDebug() << "projectDataBase::removeElement remove error : " << m_remove_element_query.lastError(); + } + + m_remove_element_info_query.bindValue(":uuid", element->uuid().toString()); + if (m_remove_element_info_query.exec()) { + changed = true; + } else { + qDebug() << "projectDataBase::removeElement remove element_info error : " << m_remove_element_info_query.lastError(); + } + + if (changed) { emit dataBaseUpdated(); } } @@ -1159,6 +1172,21 @@ void projectDataBase::prepareQuery() m_remove_element_query = QSqlQuery(m_data_base); m_remove_element_query.prepare(remove_element); + //REMOVE ELEMENT INFO + //element_info has no ON DELETE CASCADE (foreign keys aren't + //enforced by this connection), so removeElement() must clear it + //explicitly. Without this, the row is orphaned under the removed + //element's uuid, and re-adding an element with that same uuid + //later -- undo of this same removal, or a redo replaying it -- + //hits element_info's PRIMARY KEY constraint on element_uuid: the + //element re-add succeeds, but its element_info insert silently + //fails and is lost. removeDiagram()'s cascade already clears this + //table when a whole folio goes, but that does not run for a + //single element removed on its own. + QString remove_element_info("DELETE FROM element_info WHERE element_uuid=:uuid"); + m_remove_element_info_query = QSqlQuery(m_data_base); + m_remove_element_info_query.prepare(remove_element_info); + //UPDATE ELEMENT INFO QString update_str("UPDATE element_info SET "); for (auto string : QETInformation::elementInfoKeys()) { diff --git a/sources/dataBase/projectdatabase.h b/sources/dataBase/projectdatabase.h index 55aacedd5..01da46856 100644 --- a/sources/dataBase/projectdatabase.h +++ b/sources/dataBase/projectdatabase.h @@ -139,6 +139,7 @@ class projectDataBase : public QObject QSqlQuery m_insert_elements_query, m_insert_element_info_query, m_remove_element_query, + m_remove_element_info_query, m_update_element_query, m_insert_diagram_query, m_remove_diagram_query,