[][src]Derive Macro diesel::deserialize::Queryable

#[derive(Queryable)]
{
    // Attributes available to this derive:
    #[column_name]
    #[diesel]
}

Implements Queryable to load the result of statically typed queries

This trait can only be derived for structs, not enums.

When this trait is derived, it will assume that the order of fields on your struct match the order of the fields in the query. This means that field order is significant if you are using #[derive(Queryable)]. Field name has no effect.

To provide custom deserialization behavior for a field, you can use #[diesel(deserialize_as = "SomeType")]. If this attribute is present, Diesel will deserialize the corresponding field into SomeType, rather than the actual field type on your struct and then call .into to convert it to the actual field type. This can be used to add custom behavior for a single field, or use types that are otherwise unsupported by Diesel.

Attributes

Optional field attributes:

Examples

If we just want to map a query to our struct, we can use derive.

#[derive(Queryable, PartialEq, Debug)]
struct User {
    id: i32,
    name: String,
}

let first_user = users.first(&connection)?;
let expected = User { id: 1, name: "Sean".into() };
assert_eq!(expected, first_user);

If we want to do additional work during deserialization, we can use deserialize_as to use a different implementation.

struct LowercaseString(String);

impl Into<String> for LowercaseString {
    fn into(self) -> String {
        self.0
    }
}

impl<DB> Queryable<Text, DB> for LowercaseString
where
    DB: Backend,
    String: FromSql<Text, DB>
{

    type Row = String;

    fn build(s: String) -> Self {
        LowercaseString(s.to_lowercase())
    }
}

#[derive(Queryable, PartialEq, Debug)]
struct User {
    id: i32,
    #[diesel(deserialize_as = "LowercaseString")]
    name: String,
}

let first_user = users.first(&connection)?;
let expected = User { id: 1, name: "sean".into() };
assert_eq!(expected, first_user);

Alternatively, we can implement the trait for our struct manually.

use schema::users;
use diesel::deserialize::{Queryable, FromSqlRow};
use diesel::row::Row;

type DB = diesel::sqlite::Sqlite;

#[derive(PartialEq, Debug)]
struct User {
    id: i32,
    name: String,
}

impl Queryable<users::SqlType, DB> for User
where
   (i32, String): FromSqlRow<users::SqlType, DB>,
{
    type Row = (i32, String);

    fn build((id, name): Self::Row) -> Self {
        User { id, name: name.to_lowercase() }
    }
}

let first_user = users.first(&connection)?;
let expected = User { id: 1, name: "sean".into() };
assert_eq!(expected, first_user);