Skip to content

How to use custom struct with raw sql query #2691

@bcba25

Description

@bcba25

Hello everyone. I got a question on raw queries. Haven’t found much on diesel’s documentation.

#[derive(Debug, QueryableByName)]
#[table_name = "products"]
struct Product {
    pub id: Uuid,
    pub user_id: Uuid,
    pub name: String,
    pub location: Option<GeogPoint>,
    pub distance: f64, <-- ERROR: cannot find type `distance` in module `products`, not found in `products`
}

const QUERY: &str = r#"SELECT "id", "user_id", "name", "location", ST_Distance(...) as "distance" FROM products WHERE ST_Distance(...) <= 1000 ORDER BY "distance" ASC"#; 
let products = diesel::sql_query(QUERY).load::<Product>(_connection);

I’m wondering how can I use my custom structs with diesel and raw queries. If I delete distance field from struct, it will compile and execute the query but I need to show distance as well, which does not exists on table and formed with a query itself. Could you suggest on this please

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions