You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure if this is an option on add-column or a separate command like add-generated-column. Either way, it needs an argument to populate it. It could be something like this:
I like add-generated-column - feels very clear to me, and is a nice place for adding logic that checks if the DB version supports it or not and shows a useful error.
The only functional difference is that one cannot add new STORED columns using the ALTER TABLE ADD COLUMN command. Only VIRTUAL columns can be added using ALTER TABLE.
So to add stored columns to an existing table we would need to use the .transform() trick. Which implies that this should actually be a capability of the various .create() methods, since transform works by creating a new table with those and then copying across the old data.
Here's where .transform() calls .create_table_sql() under the hood:
I'm coming round to your suggestion to have this as extra arguments to sqlite-utils add-column now, especially since you also need to pass a column type.
I'd like to come up with syntax for sqlite-utils create-table as well.
Good call on adding this to create-table, especially for stored columns. Having the stored/virtual split might make this tricky to implement, but I haven't gone any farther than thinking about what the CLI looks like. I'm going to try making the SQL side work first and figure that'll tell me more about what it needs.
I tried it out in Jupyter and it works as advertised:
Introspection is a bit weird: there doesn't seem to be a way to introspect generated columns outside of parsing the stored SQL schema for the columns at the moment! And the .columns method doesn't return them at all:
This pragma returns one row for each column in the named table, including hidden columns in virtual tables. The output is the same as for PRAGMA table_info except that hidden columns are shown rather than being omitted.
One regression I found is that an unrelated rename can change the results of a numeric filter when the generated column has no declared type.
fromsqlite_utilsimportDatabasedb=Database(memory=True)
db.execute("""create table events(payload text, note text, score as (json_extract(payload, '$.score')) stored)""")
db.execute("""insert into events(payload) values ('{"score":2}'), ('{"score":10}')""")
query="select score from events where score >= 9"assertlist(db.execute(query)) == [(10,)]
db["events"].transform(rename={"note": "label"})
assertlist(db.execute(query)) == [(10,)]
The final assertion gets []. The rebuilt schema declares score TEXT, so the values sort as '10', '2'. SQLite's native RENAME COLUMN preserves the empty declared type, integer values and the matching row. I got the same results for VIRTUAL and STORED columns on SQLite 3.45.1 and 3.53.1. An explicit score INTEGER AS (...) also preserved the result on the prototype. Codex prepared a patch against this prototype that preserves the original declared type through a rebuild. With the patch, the same query returned the expected row on both SQLite versions. The full suite passed on both, and the SQLite 3.23.1 compatibility run passed with the generated-column cases skipped.
Activity
simonw commentedon Mar 11, 2022
I like
add-generated-column- feels very clear to me, and is a nice place for adding logic that checks if the DB version supports it or not and shows a useful error.simonw commentedon Mar 11, 2022
The Python API could be:
simonw commentedon Mar 11, 2022
Just spotted this in https://www.sqlite.org/gencol.html
So to add stored columns to an existing table we would need to use the
.transform()trick. Which implies that this should actually be a capability of the various.create()methods, since transform works by creating a new table with those and then copying across the old data.Here's where
.transform()calls.create_table_sql()under the hood:sqlite-utils/sqlite_utils/db.py
Lines 1627 to 1637 in 9388edf
simonw commentedon Mar 11, 2022
Two new parameters to
.create_table()and friends:generated={...}- generated column definitionsgenerated_stored={...}generated stored column definitionsThese columns will be added at the end of the table, but you can use the
column_order=parameter to apply a different order.simonw commentedon Mar 11, 2022
I'm coming round to your suggestion to have this as extra arguments to
sqlite-utils add-columnnow, especially since you also need to pass a column type.I'd like to come up with syntax for
sqlite-utils create-tableas well.https://sqlite-utils.datasette.io/en/stable/cli-reference.html#create-table
Maybe extra
--generated-stored colname expression(and--generated) options would work there.eyeseast commentedon Mar 11, 2022
Good call on adding this to
create-table, especially for stored columns. Having the stored/virtual split might make this tricky to implement, but I haven't gone any farther than thinking about what the CLI looks like. I'm going to try making the SQL side work first and figure that'll tell me more about what it needs.simonw commentedon Mar 11, 2022
I tried it out in Jupyter and it works as advertised:
Introspection is a bit weird: there doesn't seem to be a way to introspect generated columns outside of parsing the stored SQL schema for the columns at the moment! And the
.columnsmethod doesn't return them at all:sqlite-utils/sqlite_utils/db.py
Lines 1207 to 1213 in 4338136
Here's why:
So
table_xinfo()is needed to get back columns including generated columns: https://www.sqlite.org/pragma.html#pragma_table_xinfosimonw commentedon Mar 11, 2022
Trying to figure out what that extra field in
table_infocompared totable_xinfois:simonw commentedon Nov 12, 2026
The
.transform()method currently silently discards existing generated columns, which is bad!Prototype creating generated columns
StoredColumn subclass of GeneratedColumn
businessdeveloper69 commentedon Nov 21, 2026
I tried the generated-column prototype at 770c0d8 with the
itemstable from hacker-news-to-sqlite #6. On main, renamingtitledropped the virtual permalink. The prototype kept the correct URL even when I renamed its inputid.One regression I found is that an unrelated rename can change the results of a numeric filter when the generated column has no declared type.
The final assertion gets
[]. The rebuilt schema declaresscore TEXT, so the values sort as'10', '2'. SQLite's nativeRENAME COLUMNpreserves the empty declared type, integer values and the matching row. I got the same results for VIRTUAL and STORED columns on SQLite 3.45.1 and 3.53.1. An explicitscore INTEGER AS (...)also preserved the result on the prototype. Codex prepared a patch against this prototype that preserves the original declared type through a rebuild. With the patch, the same query returned the expected row on both SQLite versions. The full suite passed on both, and the SQLite 3.23.1 compatibility run passed with the generated-column cases skipped.AI assisted.