-
Hello people! I'm having an issue with sqlite and uuid's, I've asked around different discord servers with little to no success, and I've been stuck for a week with this issue, I'm quite new to rust and sqlx so I thought about opening this discussion (I do not know if it's an issue). I'm trying to use uuid with
my understanding is that a struct with a uuid should be possible to convert to uuid and back when doing a query with #[derive(FromRow, Debug)]
pub struct User {
pub uuid: uuid::Uuid,
}
async fn insert_user(pool: &SqlitePool) -> Result<User> {
let uuid = uuid::Uuid::new_v4();
let user = sqlx::query_as!(
User,
r#"
INSERT INTO users__users (uuid) VALUES ($1) RETURNING uuid
"#,
uuid,
)
.fetch_one(pool)
.await?;
Ok(user)
} Is this correct? I checked the source code and it seems so (with my little understanding): sqlx/sqlx-core/src/sqlite/types/uuid.rs Lines 20 to 35 in 2182925 I built a repository with an example, just clone it and you'll see the error: At this point I think I'm missing something, but I do not know what is it, some configuration? some trait? Please help 🙏🏻 Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@woile query_as returns a |
Beta Was this translation helpful? Give feedback.
@woile query_as returns a
String
if you usedTEXT
andVec<u8>
if you usedBLOB
. To convert it to a Uuid you have to use theSELECT uuid as "uuid: uuid::Uuid" FROM ...
syntax.Here you can find an example: https://github.com/dvob/sqlx-test/blob/1da9e0bde140c247f862c0079c74ed30d6fa1919/src/user.rs#L55
Here is the documentation: https://docs.rs/sqlx/latest/sqlx/macro.query_as.html#column-type-override-infer-from-struct-field