Skip to content

Instantly share code, notes, and snippets.

@compressed
Created May 8, 2018 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save compressed/decee7e928ed4dc10dfb094e7458e8a2 to your computer and use it in GitHub Desktop.
Save compressed/decee7e928ed4dc10dfb094e7458e8a2 to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate diesel;
use diesel::{
prelude::*, query_builder::{InsertStatement, UndecoratedInsertRecord},
query_dsl::methods::ExecuteDsl,
};
trait LoadTable {
type Insertable;
fn fetch_data() -> Vec<Self::Insertable>;
fn populate_table<T>(conn: &PgConnection, table: T)
where
T: Table,
for<'a> &'a [Self::Insertable]: UndecoratedInsertRecord<T> + Insertable<T>,
for<'a> InsertStatement<T, <&'a [Self::Insertable] as Insertable<T>>::Values>:
ExecuteDsl<PgConnection>,
{
let data = Self::fetch_data();
data.insert_into(table).execute(conn).unwrap();
}
}
table! {
blah (id) {
id -> Int4,
value -> Int4,
}
}
#[derive(Debug, Insertable)]
#[table_name = "blah"]
struct Blah {
value: i32,
}
struct A;
impl LoadTable for A {
type Insertable = Blah;
fn fetch_data() -> Vec<Self::Insertable> {
vec![Blah { value: 1 }]
}
}
fn main() {
let conn = PgConnection::establish("abc").unwrap();
A::populate_table(&conn, blah::table);
}
// returns...
// [Running cargo check]
// Checking diesel_bound v0.1.0
// error[E0271]: type mismatch resolving `for<'a> <std::option::Option<diesel::expression::operator
// s::Eq<blah::columns::value, diesel::expression::bound::Bound<diesel::sql_types::Integer, &'a i32
// >>> as diesel::Insertable<blah::table>>::Values == diesel::query_builder::ValuesClause<_, blah::
// table>`
// --> src/main.rs:50:5
// |
// 50 | A::populate_table(&conn, blah::table);
// | ^^^^^^^^^^^^^^^^^ expected bound lifetime parameter 'a, found concrete lifetime
// |
// = note: required because of the requirements on the impl of `diesel::Insertable<blah::table>`
// for `(std::option::Option<diesel::expression::operators::Eq<blah::columns::value, diesel::expre
// ssion::bound::Bound<diesel::sql_types::Integer, &i32>>>,)`
// = note: required because of the requirements on the impl of `for<'a> diesel::query_builder::Q
// ueryFragment<diesel::pg::Pg>` for `diesel::insertable::BatchInsert<'a, Blah, blah::table>`
// = note: required because of the requirements on the impl of `for<'a> diesel::query_builder::Q
// ueryFragment<diesel::pg::Pg>` for `diesel::query_builder::InsertStatement<blah::table, diesel::i
// nsertable::BatchInsert<'a, Blah, blah::table>>`
// = note: required because of the requirements on the impl of `for<'a> diesel::query_dsl::load_
// dsl::ExecuteDsl<diesel::PgConnection, diesel::pg::Pg>` for `diesel::query_builder::InsertStateme
// nt<blah::table, <&'a [Blah] as diesel::Insertable<blah::table>>::Values>`
// note: required by `LoadTable::populate_table`
// --> src/main.rs:14:5
// |
// 14 | / fn populate_table<T>(conn: &PgConnection, table: T)
// 15 | | where
// 16 | | T: Table,
// 17 | | for<'a> &'a [Self::Insertable]: UndecoratedInsertRecord<T> + Insertable<T>,
// ... |
// 22 | | data.insert_into(table).execute(conn).unwrap();
// 23 | | }
// | |_____^
//
// error: aborting due to previous error
// //
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment