Leave generated columns out of Export to SQL - #4198
Open
anandghegde wants to merge 1 commit into
Open
anandghegde wants to merge 1 commit into
anandghegde wants to merge 1 commit into
Conversation
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 sqlitebrowser#2978
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2978.
Export → Database to SQL file writes every column of a table into its INSERT statements, generated columns included. SQLite doesn't allow inserting into a generated column, so the exported file can't be imported again:
INSERT INTO "orders" VALUES (1,2,9.5,19.0,'order 1','O''Brien');fails with table orders has 4 columns but 6 values were supplied.On #2978 it was suggested that the export should keep all columns. The sqlite3 shell's
.dumpleaves generated columns out (INSERT INTO t VALUES(1);), because SQLite recomputes them on import. This change makes DB4S behave the same way, so its SQL export can be re-imported.Change
In
DBBrowserDB::dump(), tables that have at least one generated column select, and name in the INSERT statements, only their other columns. Tables without generated columns keep the existingSELECT *, so their export is byte-for-byte unchanged. The same file already skips generated columns this way inemptyInsertStmt()and when copying data duringalterTable().Test plan
Built on macOS (Apple Silicon, Qt 5.15.16 from the project tap) with
-DENABLE_TESTING=ON.ctestpasses (4/4). None of those tests cover the export.Exported the
orderstable above, which has a stored and a virtual generated column, a quoted value and a NULL, together with a plain table. I used each export option and re-imported every file into a fresh database withsqlite3:ordersINSERT INTO "orders" VALUES (1,2,9.5,'O''Brien');INSERT INTO "orders" ("id","qty","price","note") VALUES (...)INSERT INTO "orders" VALUES (1,2,9.5,'O''Brien'),\n (2,3,1.25,NULL);The plain table exports as before in all three.
CSV import into an existing table with generated columns has a similar problem (
ImportCsvDialog.cpp). I've kept this PR to the SQL export and can send that as a separate PR if it's wanted.