Diesel implement a trait how to?

Hi Everyone,
I try to use Diesel as ORM with Rust
I have created the model which is

#[derive( Debug, Identifiable)]
#[table_name = "myapp_carbrand"]
pub struct MyappCarbrand {
    pub id: i32,
    pub brand: String,

Here is the schema 

table! {
    myapp_carbrand (id) {
        id -> Int4,
        brand -> Varchar,

impl Queryable<myapp_carbrand::SqlType, DB> for MyappCarbrand {
    type Row = (i32, String);

    fn build(row: Self::Row) -> Self {
        MyappCarbrand {
            id: row.0,
            brand: row.1,

Here is the main program

fn main() {
    use schema::myapp_carbrand::dsl::*;
    let connection = establish_connection();
    let results = myapp_carbrand.select(id)
        .expect("Error loading posts");

    println!("Displaying {:?} posts", results);
    for _post in results {
        println!("{:?}", _post);
        //println!("{:?}", _post);

I have the error message during the compilation

   |          ^^^^ the trait `diesel::Queryable<diesel::sql_types::Integer, _>` is not implemented for `models::MyappCarbrand`
   = help: the following implementations were found:
             <models::MyappCarbrand as diesel::Queryable<(diesel::sql_types::Integer, diesel::sql_types::Text), diesel::pg::Pg>>
   = note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<_, models::MyappCarbrand>` for `diesel::query_builder::SelectStatement<schema::myapp_carbrand::table, diesel::query_builder::select_clause::SelectClause<schema::myapp_carbrand::columns::id>>`

As I am new to Rust Please could you help me to solve the issue


You cannot map the result of an query that returns one field (because of .select(id)) to a struct that can be constructed only with two fields (because of your Queryable impl, Row contains two fields). You either need to change the type the result should be loaded into into something different (for example just i32) or change your query to return all fields (by removing the explicit select)

Thank you
It works, I saw that the select clause only allow one parameter
Do you know how to select only several fields in the record description



@DidierBoivin You can pass a tuple to select multiple fields:

select((id, brand))

Since select takes an expression as the first argument: https://docs.rs/diesel/1.4.5/diesel/expression/trait.Expression.html

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.