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
{{ message }}
Repository navigation
Add support for typing using sqlite RETURNING clause #1246
structRowid{rowid:i64,}#[async_std::main]asyncfnmain(){let pool = sqlx::SqlitePool::connect("file::memory:").await.unwrap();
sqlx::query!("CREATE TABLE IF NOT EXISTS test (rowid INTEGER PRIMARY KEY);").execute(&pool).await.unwrap();
sqlx::query_as!(Rowid,"INSERT INTO test (rowid) VALUES (42) RETURNING rowid").fetch_one(&pool).await.unwrap();}
this with a test database with the first statement executed will yield error: unsupported type NULL of column #1 ("rowid")
This works in the above example, but not my intended (and probably still valid) case:
I actually wanted to do something similar and seemingly oversimplified the problem:
asyncfnmain(){let pool = sqlx::SqlitePool::connect("file::memory:").await.unwrap();
sqlx::query!("CREATE TABLE IF NOT EXISTS test (rowid INTEGER PRIMARY KEY, name TEXT);").execute(&pool).await.unwrap();
sqlx::query_as!(Rowid,r#"INSERT INTO test (name) VALUES ('42') RETURNING rowid as "rowid: i64""#).fetch_one(&pool).await.unwrap();}
In this case, it expects a Option<i64> although the field is a primary key.
It's likely the same issue, the column is assumed to be nullable unless we can prove that it won't be and there seems to be something confusing the heuristics here; if we can't even determine the type of the column, we certainly can't determine its nullability.
One question though, with your "trick" is there a way to get back the whole struct?
I guess the best thing to do in that case is to just do another query to get the newly inserted record, correct?
This is basically an unreachable panic as sqlx assumes that every return value is an option until it can prove it wrong. So in this case, this field will never be an None value, but sqlx wants to be on the safe side.
Thank you! unfortunately, you need to create a new struct with all the fields that are options and then you can return that. So in the end, rather than creating a new structs, making a new query seems a little simpler.
Thank you for the info about the unreacheable panic!
Activity
abonander commentedon May 25, 2021
This is likely an issue with our bytecode analysis pass, but you can use the type override syntax which should let you bypass this error:
fabi321 commentedon May 25, 2021
This works in the above example, but not my intended (and probably still valid) case:
I actually wanted to do something similar and seemingly oversimplified the problem:
In this case, it expects a
Option<i64>although the field is a primary key.I'm not sure if I'm missing a scenario here.
abonander commentedon May 25, 2021
It's likely the same issue, the column is assumed to be nullable unless we can prove that it won't be and there seems to be something confusing the heuristics here; if we can't even determine the type of the column, we certainly can't determine its nullability.
happysalada commentedon Jul 7, 2021
Thank you for the workaround, here is what I ended up doing
You do have to return a result in the end, but it seems to me that this can indicate a failure to insert for example.
happysalada commentedon Jul 7, 2021
One question though, with your "trick" is there a way to get back the whole struct?
I guess the best thing to do in that case is to just do another query to get the newly inserted record, correct?
fabi321 commentedon Jul 7, 2021
The ? here catches an failed insertion.
This is basically an unreachable panic as sqlx assumes that every return value is an option until it can prove it wrong. So in this case, this field will never be an None value, but sqlx wants to be on the safe side.
fabi321 commentedon Jul 7, 2021
As by the Sqlite reference, you can return all fields of the table you're inserting into.
happysalada commentedon Jul 7, 2021
Thank you! unfortunately, you need to create a new struct with all the fields that are options and then you can return that. So in the end, rather than creating a new structs, making a new query seems a little simpler.
Thank you for the info about the unreacheable panic!
abonander commentedon Jul 21, 2021
Closed by #1323