Skip to content

Commit

Permalink
Problems with WITHOUT ROWID tables with PK of string type
Browse files Browse the repository at this point in the history
This fixes two related problems:
- When the PK is updated the hidden column 0 must be updated too,
  otherwise any further editing of the same row before a table refresh is
  broken.
- When a new record is inserted and the PK has string type we cannot
  simply make a pseudo auto-increment and insert that value as rowid
  because that added key would be impossible to update because our
  UPDATE clause will try to update a column with a string and it contains
  an integer. In this case it's better to let the user enter the PK value,
  so the new Add Record dialog is directly invoked.

See issue #1332 and (tangentially) #1049. The first should be fixed now.
The later not, but at least there is now a workaround: removing the
AUTOINCREMENT option and use the WITHOUT ROWID one.
  • Loading branch information
mgrojo committed Oct 6, 2018
1 parent 8795ba5 commit e6392dc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/MainWindow.cpp
Expand Up @@ -775,11 +775,13 @@ void MainWindow::closeEvent( QCloseEvent* event )
void MainWindow::addRecord()
{
int row = m_browseTableModel->rowCount();
if(m_browseTableModel->insertRow(row))
bool isWithoutRowidTable = db.getObjectByName(currentlyBrowsedTableName())->type() == sqlb::Object::Table && db.getObjectByName<sqlb::Table>(currentlyBrowsedTableName())->isWithoutRowidTable();

if(!isWithoutRowidTable && m_browseTableModel->insertRow(row))
{
selectTableLine(row);
} else {
// Error inserting empty row.
// Table without rowid (let user enter value for PK) or error inserting empty row.
// User has to provide values acomplishing the constraints. Open Add Record Dialog.
insertValues();
}
Expand Down
6 changes: 6 additions & 0 deletions src/sqlitetablemodel.cpp
Expand Up @@ -393,6 +393,12 @@ bool SqliteTableModel::setTypedData(const QModelIndex& index, bool isBlob, const
{
cached_row.replace(index.column(), newValue);
lock.unlock();
// Special case for rowid columns
if(m_headers.at(index.column()) == m_sRowidColumn) {
cached_row.replace(0, newValue);
const QModelIndex& rowidIndex = index.sibling(index.row(), 0);
emit dataChanged(rowidIndex, rowidIndex);
}
emit dataChanged(index, index);
return true;
} else {
Expand Down

0 comments on commit e6392dc

Please sign in to comment.