Skip to content

Commit 106d7cb

Browse files
committed
Leave generated columns out of Export to SQL
Export > Database to SQL file wrote every column into the INSERT statements, including generated columns. SQLite refuses to insert into a generated column, so importing the file failed with "table X has N columns but M values were supplied", or with "cannot INSERT into generated column" when column names were kept. Only for tables that have generated columns, select and insert the other columns instead, the same as the sqlite3 shell's .dump. The generated values are recomputed on import. Other tables still export with SELECT *. Fixes #2978
1 parent 9ccf550 commit 106d7cb

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

‎src/sqlitedb.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,19 @@ bool DBBrowserDB::dump(const QString& filePath,
990990
// get columns
991991
sqlb::StringVector cols = it->fieldNames();
992992

993+
// Generated columns cannot be inserted into, so leave them out of both the SELECT and the
994+
// INSERT statements, like the sqlite3 shell's .dump does
993995
std::string sQuery = "SELECT * FROM " + sqlb::escapeIdentifier(it->name());
996+
if(std::any_of(it->fields.begin(), it->fields.end(), [](const sqlb::Field& f) { return f.generated() != nullptr; }))
997+
{
998+
cols.clear();
999+
for(const sqlb::Field& f : it->fields)
1000+
{
1001+
if(!f.generated())
1002+
cols.push_back(f.name());
1003+
}
1004+
sQuery = "SELECT " + sqlb::joinStringVector(sqlb::escapeIdentifier(cols), ",") + " FROM " + sqlb::escapeIdentifier(it->name());
1005+
}
9941006
sqlite3_stmt *stmt;
9951007
QString lineSep(QString(")%1\n").arg(insertNewSyntx?',':';'));
9961008

0 commit comments

Comments
 (0)