How to make generic function with `impl diesel::Connection`

I am trying to build a transaction function insert_job which looks like the following:
Example

use diesel::{insert_into, Connection, RunQueryDsl};

use super::{
    models::{ConverterResult, DbJob, JobStatus},
    schema::jobs::dsl::*,
};
use crate::job::JobBundle;

fn insert_job<C>(conn: &mut C, job: &JobBundle)
where
    C: Connection,
{
    let j = DbJob {
        id: job.id,
        status: JobStatus::Idle,
        blob_digest: job.blob_digest.clone(),
        name: job.name.clone(),
        converter_result: ConverterResult::None,
        converter_log: "".to_owned(),
    };

    insert_into(jobs).values(j).execute(conn);
}

The execute(conn) actually is excatly the same written as I did but Rust wants me to put all sorts of bounds on the Connection (why?):

error[E0277]: the trait bound `<C as diesel::Connection>::Backend: DieselReserveSpecialization` is not satisfied
    --> components/common/src/db/transactions.rs:21:41
     |
21   |     insert_into(jobs).values(j).execute(conn);
     |                                 ------- ^^^^ the trait `DieselReserveSpecialization` is not implemented for `<C as diesel::Connection>::Backend`
     |                                 |
     |                                 required by a bound introduced by this call
     |
     = note: required for `InsertStatement<table, ValuesClause<..., ...>>` to implement `QueryFragment<<C as diesel::Connection>::Backend>`
     = note: the full type name has been written to '/home/nixos/Desktop/Repos/markdown-to-pdf-service/target/debug/deps/common-407478c3fbb79c9d.long-type-15797717890643199462.txt'
     = note: required for `InsertStatement<table, ValuesClause<..., ...>>` to implement `ExecuteDsl<C>`
     = note: the full type name has been written to '/home/nixos/Desktop/Repos/markdown-to-pdf-service/target/debug/deps/common-407478c3fbb79c9d.long-type-15797717890643199462.txt'
note: required by a bound in `diesel::RunQueryDsl::execute`
    --> /home/nixos/.cargo/registry/src/index.crates.io-6f17d22bba15001f/diesel-2.1.4/src/query_dsl/mod.rs:1432:15
     |
1429 |     fn execute(self, conn: &mut Conn) -> QueryResult<usize>
     |        ------- required by a bound in this associated function
...
1432 |         Self: methods::ExecuteDsl<Conn>,
     |               ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RunQueryDsl::execute`
help: consider further restricting the associated type
     |
11   |     C: Connection, <C as diesel::Connection>::Backend: DieselReserveSpecialization
     |                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0277]: the trait bound `DefaultValues: QueryFragment<<C as diesel::Connection>::Backend, <<C as diesel::Connection>::Backend as SqlDialect>::DefaultValueClauseForInsert>` is not satisfied
    --> components/common/src/db/transactions.rs:21:41

and the list goes on and on...

Does anybody know where I have to watch out when writting such a generic function. I thougth that works, but it seems totally nontrivial.

Example

Crosspost in How to compile function with `impl diesel::Connection`? · diesel-rs/diesel · Discussion #3909 · GitHub where the problem seems to have been solved for OP by diesel's maintainer.

2 Likes

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.