{"id":1219,"hash":"8eba1ca08c3c1c4960b27e327b27b3cd4a2adb537d71f8acc747d6ea69b505a4","pattern":"Rust Diesel, error loading results of a sql_query that select columns from multiple tables","full_message":"I tried to execute an sql_query with Diesel that doesn't match a single table and I got the following error:\n\n    error[E0277]: the trait bound `Untyped: load_dsl::private::CompatibleType<TimeCountSumaryEntry, _>` is not satisfied\n        --> src/api/cra_service.rs:263:10\n         |\n    263  |         .load::<TimeCountSumaryEntry>(connection)\n         |          ^^^^ the trait `load_dsl::private::CompatibleType<TimeCountSumaryEntry, _>` is not implemented for `Untyped`\n         |\n         = help: the trait `load_dsl::private::CompatibleType<U, DB>` is implemented for `Untyped`\n         = note: required because of the requirements on the impl of `LoadQuery<'_, _, TimeCountSumaryEntry>` for `SqlQuery`\n    note: required by a bound in `diesel::RunQueryDsl::load\n\nHere is the relevant code (where clauses are fixed to simplify experimentations):\n\n#[derive(QueryableByName, Debug)]\nstruct TimeCountSumaryEntry {\n    #[diesel(sql_type = Integer)]\n    month: i32,\n    #[diesel(sql_type = Integer)]\n    year: i32,\n    #[diesel(sql_type = Integer)]\n    project_id: i32,\n    #[diesel(sql_type = Text)]\n    project_code: String,\n    #[diesel(sql_type = Double)]\n    time_spent: f32,\n    #[diesel(sql_type = Text)]\n    fullname: String,\n}\n\nfn _timecount_by_filters(\n    user_id: Option<i32>,\n    month: Option<u8>,\n    year: Option<u16>,\n    connection: &mut PgConnection,\n) {\n    let query =\n        \"SELECT\n            EXTRACT(MONTH FROM tc.date_assigned) as \\\"month\\\",\n            EXTRACT(YEAR FROM tc.date_assigned) as \\\"year\\\",\n            tc.project_id as project_id,\n            p.project_code as project_code,\n            sum(tc.time_spent) as time_spent,\n            u.lastname || ' ' || u.firstname as fullname\n        FROM\n            time_count tc\n            JOIN cra c on tc.cra_id = c.cra_id\n            JOIN project p on p.project_id = tc.project_id\n            JOIN \\\"user\\\" u on u.user_id = c.user_id\n        WHERE\n            u.user_id = 3\n            and EXTRACT(MONTH FROM tc.date_assigned) = 8\n            and EXTRACT(YEAR FROM tc.date_assigned) = 2022\n        GROUP BY\n            tc.project_id, u.lastname, u.firstname, \\\"month\\\", \\\"year\\\", p.project_code\n        ORDER BY\n            \\\"year\\\", \\\"month\\\", u.lastname, u.firstname, tc.project_id\";\n\n    let time_counts_sumary = diesel::dsl::sql_query(query)\n        .load::<TimeCountSumaryEntry>(connection)\n        .expect(\"Error getting cra ids\");\n    println!(\"{:?}\", time_counts_sumary);\n}\n\nI can't find any resource that mention how to deal with this use case (or even that this isn't possible at all). I first tried with the query builder, but it didn't seem possible, so I thought sql_query the way to get those data from DB (postgresql) without getting useless information in the process, but maybe there is a better one.\n\nDoes anyone have encountered this use case or have any hints about how to deal with it ?","ecosystem":"cargo","package_name":"rust-diesel","package_version":null,"solution":"If someone have the same problem, I found where it was.\n\nDiesel couldn't match types it got from sql_query and what I mentioned in TimeCountSumaryEntry.\n\nFirst here:\n\n#[diesel(sql_type = Double)]\ntime_spent: f32,\n\nI mistyped the Double (it should be f64)\n\nAnd second the EXTRACT() function return a numeric in postgresql.\nI could use the numeric diesel feature, but in my case (year and month), an Integer is enough. So I must specify the type in the request with CAST(). Like this:\n\nCAST(EXTRACT(MONTH FROM tc.date_assigned) as Integer) as \"month\",\nCAST(EXTRACT(YEAR FROM tc.date_assigned) as Integer) as \"year\",\n\nHere is the working code:\n\n#[derive(QueryableByName, Debug)]\nstruct TimeCountSumaryEntry {\n    #[diesel(sql_type = Integer)]\n    month: i32,\n    #[diesel(sql_type = Integer)]\n    year: i32,\n    #[diesel(sql_type = Integer)]\n    project_id: i32,\n    #[diesel(sql_type = Text)]\n    project_code: String,\n    #[diesel(sql_type = Double)]\n    time_spent: f64,\n    #[diesel(sql_type = Text)]\n    fullname: String,\n}\n\nfn _timecount_by_filters(\n    user_id: Option<i32>,\n    month: Option<u8>,\n    year: Option<u16>,\n    connection: &mut PgConnection,\n) {\n    let query =\n        \"SELECT\n            CAST(EXTRACT(MONTH FROM tc.date_assigned) as integer) as \\\"month\\\",\n            CAST(EXTRACT(YEAR FROM tc.date_assigned) as integer) as \\\"year\\\",\n            tc.project_id as project_id,\n            p.project_code as project_code,\n            sum(tc.time_spent) as time_spent,\n            u.lastname || ' ' || u.firstname as fullname\n        FROM\n            time_count tc\n            JOIN cra c on tc.cra_id = c.cra_id\n            JOIN project p on p.project_id = tc.project_id\n            JOIN \\\"user\\\" u on u.user_id = c.user_id\n        WHERE\n            u.user_id = 3\n            and EXTRACT(MONTH FROM tc.date_assigned) = 8\n            and EXTRACT(YEAR FROM tc.date_assigned) = 2022\n        GROUP BY\n            tc.project_id, u.lastname, u.firstname, \\\"month\\\", \\\"year\\\", p.project_code\n        ORDER BY\n            \\\"year\\\", \\\"month\\\", u.lastname, u.firstname, tc.project_id\";\n\n    let time_counts_sumary = diesel::dsl::sql_query(query)\n        .load::<TimeCountSumaryEntry>(connection)\n        .expect(\"Error getting cra ids\");\n    println!(\"{:?}\", time_counts_sumary);\n}","confidence":0.85,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/73645993/rust-diesel-error-loading-results-of-a-sql-query-that-select-columns-from-multi","votes":6,"created_at":"2026-04-19T04:52:37.217229+00:00","updated_at":"2026-04-19T04:52:37.217229+00:00"}