title: Understanding trait bound error in Diesel
body: <p>I want to write a function that will insert a type into a database where the database connection parameter is generic, so that it can work on multiple backends.</p>
  
  <p>I came up with the following function to insert an object using a generic connection:</p>
  
  <pre><code>pub fn create_label&lt;C&gt;(connection: &amp;C, label: &amp;model::Label)
  where
      C: Connection,
      C::Backend: diesel::backend::Backend,
      C::Backend: diesel::backend::SupportsDefaultKeyword,
  {
      diesel::insert(&amp;label)
          .into(schema::label::table)
          .execute(connection);
  }
  </code></pre>
  
  <p>If I don't include the <code>SupportsDefaultKeyword</code> constraint, the function will not compile. When calling it with a <code>SqliteConnection</code> as the connection parameter, I get the following error:</p>
  
  <blockquote>
    <p><code>database::create_label(&amp;db_conn, &amp;label);
                         ^^^^^^^^^^^^^^^^^^^^^^ the trait
        'diesel::backend::SupportsDefaultKeyword' is not implemented for
        'diesel::sqlite::Sqlite'</code></p>
  </blockquote>
  
  <p>This would imply that inserting data with a <code>SqliteConnection</code> does not work. That's obviously not the case, and furthermore changing <code>create_label</code> such that it takes a <code>SqliteConnection</code> directly works just fine.</p>
  
  <pre><code>pub fn create_label(connection: &amp;SqliteConnection, label: &amp;model::Label) {
      diesel::insert(&amp;label)
          .into(schema::label::table)
          .execute(connection);
  }
  </code></pre>
  
  <p>Why is it that the generic function requires the <code>SupportsDefaultKeyword</code> constraint and the function taking <code>SqliteConnection</code> does not?</p>
  
  <p>Here is a <a href="https://play.integer32.com/?gist=878d86b53dce6a11e317f9cf08cfdb74&amp;version=stable" rel="nofollow noreferrer">minimal example</a> illustrating the problem. As per the comments, line 60 of <code>main.rs</code> will not compile with the error from above, whereas line 61 does compile:</p>
  
  <pre><code>#[macro_use]
  extern crate diesel;
  #[macro_use]
  extern crate diesel_codegen;
  
  mod schema {
      table! {
          labels {
              id -&gt; Integer,
              name -&gt; VarChar,
          }
      }
  }
  
  mod model {
      use schema::labels;
  
      #[derive(Debug, Identifiable, Insertable)]
      #[table_name = "labels"]
      pub struct Label {
          pub id: i32,
          pub name: String,
      }
  }
  
  use diesel::ExecuteDsl;
  use diesel::Connection;
  use diesel::prelude::*;
  use diesel::sqlite::SqliteConnection;
  
  pub fn create_label&lt;C&gt;(connection: &amp;C, label: &amp;model::Label)
  where
      C: Connection,
      C::Backend: diesel::backend::Backend,
      C::Backend: diesel::backend::SupportsDefaultKeyword,
  {
      diesel::insert(label)
          .into(schema::labels::table)
          .execute(connection)
          .expect("nope");
  }
  
  pub fn create_label_sqlite(connection: &amp;SqliteConnection, label: &amp;model::Label) {
      diesel::insert(label)
          .into(schema::labels::table)
          .execute(connection)
          .expect("nope");
  }
  
  pub fn establish_connection() -&gt; SqliteConnection {
      let url = "test.db";
      SqliteConnection::establish(&amp;url).expect(&amp;format!("Error connecting to {}", url))
  }
  
  fn main() {
      let label = model::Label {
          id: 1,
          name: String::from("test"),
      };
      let conn = establish_connection();
  
      create_label(&amp;conn, &amp;label); /* Does not compile */
      create_label_sqlite(&amp;conn, &amp;label); /*Compiles */
  }
  </code></pre>
  
  <pre><code>[dependencies]
  diesel = { version = "0.16.0", features = ["sqlite"] }
  diesel_codegen = "0.16.0"
  </code></pre>