Repository navigation
Replies: 2 comments 3 replies
|
how do i use jsonb() when insert data? like this |
|
As the author of Diesel's SQLite JSONB support (diesel-rs/diesel#4284), I ran into this same gap from the SeaORM side on a production codebase and want to share a recipe that works end-to-end today — and answer @doveq's question about using TL;DR: there's still no native SQLite-JSONB-at-rest in SeaORM (nor in sqlx — see launchbadge/sqlx#4101), but you can get it without touching entity codegen, exactly along the loose-typing lines the OP anticipated. The approach1. Declare the column with TEXT affinity, not BLOB. SeaORM has no BLOB-default custom column type — but you don't need one. A // migration
ColumnDef::new(Packet::Properties)
.string() // TEXT affinity => entity stays String
.not_null()
.check(Expr::cust("json_valid(properties, 8)")) // flag 8 = strict JSONB2. Wrap // write: bind jsonb(?) instead of a plain ActiveValue::Set(text)
Func::cust(Alias::new("jsonb")).arg(Expr::val(serialized_json)) // Some(text) => jsonb(?)
// ...and bind a plain SQL NULL for None — never jsonb('null')
// read: project json(col) back to text so the String / serde_json round-trip is unchanged
Func::cust(Alias::new("json")).arg(Expr::col(Column::Properties))Since an insert needs a per-column expression and A gotcha that isn't in any docsIf you filter these columns with the JSON/string/array functions (not just Why do this rather than a native binary typeDiesel's SQLite A first-class Researched and drafted with the help of an AI assistant (Claude Code); the recipe is distilled from a working production implementation and its passing tests. |
Uh oh!
There was an error while loading. Please reload this page.
Heyo! Ever since SQLite 3.45.0, there is native support for JSONB on-disk and can be switched in and out on the fly to textual JSON. If using sqlite-side JSON ops, it performs faster than standard JSON, which is nice for raw queries.
Note, the actual format isn't really any smaller than regular JSON for most data that is to be held. It is smaller for a few primitives (null, true and false each only take 1 byte) and most values - even ints and floats - encode the same as standard JSON. This just moves the payload up to the start of the value on-disk.
In cases where the byte (ascii) size of the value is less than 11 bytes in length, it will be smaller - only a single byte header. But in cases where it's 12 or more, it has no actual size gains.
It can be transparently upgraded in the case of sqlite, as the function
json(x)can take both JSON and JSONB internally, and same with thejsonb(x)function to turn it into jsonb itself, so it'd be a matter of wrapping any inserts injsonb()and any loads injson()- so adding this imposes no breakages to existing on-disk data, with the exception that raw queries sitting outside of the ORM/query builder that are expecting text may no longer function as expected.There is no way to define this as a custom column type here that has a default affinity of BLOB, but thanks to sqlite's loose typing rules you can reuse the same type - or just do a custom one (
jsonb_blobor something) and it'll sort itself out, as per the column/data affinity rules.Information can be found here:
https://sqlite.org/jsonb.html
https://sqlite.org/json1.html#jminib
All reactions