-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for typing using sqlite RETURNING clause #1246
Comments
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: sqlx::query_as!(
Rowid,
r#"INSERT INTO test (rowid) VALUES (42) RETURNING rowid AS "rowid: i64""#
).fetch_one(&pool).await.unwrap(); |
This works in the above example, but not my intended (and probably still valid) case: async fn main() {
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 I'm not sure if I'm missing a scenario here. |
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. |
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. |
One question though, with your "trick" is there a way to get back the whole struct? |
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. |
As by the Sqlite reference, you can return all fields of the table you're inserting into. |
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! |
Closed by #1323 |
minimal steps to reproduce:
this with a test database with the first statement executed will yield
error: unsupported type NULL of column #1 ("rowid")
sqlx version: 0.5.5
libsqlite3 version: 3.35.4 (libsqlite3-sys 0.22.2)
The text was updated successfully, but these errors were encountered: