Can't use Uuid in a sqlx/async-graphql project

Hello everyone. I am very new to Rust and am trying out async-graphql and sqlx for a small book app. I have the following example:

use crate::gql::AppContext;
use async_graphql::{Context, Object, SimpleObject};
use sqlx::{postgres::PgRow, Row};
use uuid::Uuid;

#[derive(sqlx::FromRow, Hash, Clone, SimpleObject)]
pub struct Book {
    pub id: Uuid,
    pub title: String,
}

#[derive(Default)]
pub(crate) struct TextQuery;

#[Object]
impl TextQuery {
    async fn texts(&self, ctx: &Context<'_>) -> Result<Vec<Book>, async_graphql::Error> {
        let pool = &ctx.data::<AppContext>()?.pool;

        let query: Vec<Book> = sqlx::query("SELECT * FROM texts ORDER BY id")
            .map(|row: PgRow| Book {
                id: row.get("id"),
                title: row.get("title"),
            })
            .fetch_all(pool)
            .await?;

        Ok(query)
    }
}

However, row.get("id") gives the following error:

the trait bound `uuid::Uuid: sqlx::Decode<'_, Postgres>` is not satisfied
the following other types implement trait `sqlx::Decode<'r, DB>`:
  `&'r [u8]` implements `sqlx::Decode<'r, Postgres>`
  `&'r serde_json::raw::RawValue` implements `sqlx::Decode<'r, DB>`
  `&'r str` implements `sqlx::Decode<'r, Postgres>`
  `()` implements `sqlx::Decode<'r, Postgres>`
  `(T1, T2)` implements `sqlx::Decode<'r, Postgres>`
  `(T1, T2, T3)` implements `sqlx::Decode<'r, Postgres>`
  `(T1, T2, T3, T4)` implements `sqlx::Decode<'r, Postgres>`
  `(T1, T2, T3, T4, T5)` implements `sqlx::Decode<'r, Postgres>`
and 26 others

I looked around for help with this error and, according to this other issue, it seems that it is usually fixed by adding the uuid feature to sqlx. However, I have done this already. Here is my Cargo.toml:

[package]
name = "abadia-be"
version = "0.1.0"
edition = "2021"

[dependencies]
async-graphql = { version = "6.0.6", features = ["dataloader", "uuid"] }
async-graphql-axum = "6.0.6"
axum = "0.6.20"
dotenv = "0.15.0"
metrics = "0.21.1"
metrics-exporter-prometheus = "0.12.1"
serde = { version = "1.0.188", features = ["derive"] }
sqlx = { version = "0.5", features = ["postgres", "runtime-tokio-native-tls", "uuid"] }
tokio = { version = "1.32.0", features = ["full"] }
uuid = { version = "1.11.0", features = ["v4", "serde"] }

# retrieved from https://github.com/launchbadge/sqlx/issues/2418
[build-dependencies]
syn = "1"

Adding the feature has not solved my problem. I am at a dead end here. Does anyone know what I am missing? Thanks!

EDIT: Here is a screenshot for visibility:

You probably have a mismatch between the Uuid type that your sqlx version expects and the one you have from the uuid crate. I recommend you to upgrade sqlx to the latest version (0.8.2).

1 Like