Skip to content

Instantly share code, notes, and snippets.

@huin
Last active August 26, 2022 07:41
Show Gist options
  • Save huin/3f81c4dd5b4b462083d06306ac4785b3 to your computer and use it in GitHub Desktop.
Save huin/3f81c4dd5b4b462083d06306ac4785b3 to your computer and use it in GitHub Desktop.
Attempt to generically insert a row in Diesel with returned result
pub fn new_item_inner<'a, Table, NewModel, Model>(
db: &diesel::PgConnection,
table: Table,
new_item: &'a NewModel,
) -> QueryResult<Model>
where
Table: diesel::Table,
&'a NewModel: Insertable<Table>,
InsertStatement<Table, NewModel>: LoadQuery<PgConnection, Model>,
{
insert_into(table).values(new_item).get_result(db)
}
error[E0277]: the trait bound `<Table as diesel::QuerySource>::FromClause: diesel::query_builder::QueryFragment<_>` is not satisfied
--> campsched_web/src/api.rs:97:45
|
97 | insert_into(table).values(new_item).get_result(db)
| ^^^^^^^^^^ the trait `diesel::query_builder::QueryFragment<_>` is not implemented for `<Table as diesel::QuerySource>::FromClause`
|
= help: the following implementations were found:
<&'a T as diesel::query_builder::QueryFragment<DB>>
= note: required because of the requirements on the impl of `diesel::query_builder::QueryFragment<_>` for `diesel::query_builder::InsertStatement<Table, <&NewModel as diesel::Insertable<Table>>::Values, diesel::query_builder::insert_statement::Insert, diesel::query_builder::returning_clause::ReturningClause<<Table as diesel::Table>::AllColumns>>`
= note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<_, _>` for `diesel::query_builder::InsertStatement<Table, <&NewModel as diesel::Insertable<Table>>::Values>`
error[E0277]: the trait bound `<&NewModel as diesel::Insertable<Table>>::Values: diesel::query_builder::QueryFragment<_>` is not satisfied
--> campsched_web/src/api.rs:97:45
|
97 | insert_into(table).values(new_item).get_result(db)
| ^^^^^^^^^^ the trait `diesel::query_builder::QueryFragment<_>` is not implemented for `<&NewModel as diesel::Insertable<Table>>::Values`
|
= help: the following implementations were found:
<&'a T as diesel::query_builder::QueryFragment<DB>>
= note: required because of the requirements on the impl of `diesel::query_builder::QueryFragment<_>` for `diesel::query_builder::InsertStatement<Table, <&NewModel as diesel::Insertable<Table>>::Values, diesel::query_builder::insert_statement::Insert, diesel::query_builder::returning_clause::ReturningClause<<Table as diesel::Table>::AllColumns>>`
= note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<_, _>` for `diesel::query_builder::InsertStatement<Table, <&NewModel as diesel::Insertable<Table>>::Values>`
error[E0277]: the trait bound `<&NewModel as diesel::Insertable<Table>>::Values: diesel::insertable::CanInsertInSingleQuery<_>` is not satisfied
--> campsched_web/src/api.rs:97:45
|
97 | insert_into(table).values(new_item).get_result(db)
| ^^^^^^^^^^ the trait `diesel::insertable::CanInsertInSingleQuery<_>` is not implemented for `<&NewModel as diesel::Insertable<Table>>::Values`
|
= help: the following implementations were found:
<&'a T as diesel::insertable::CanInsertInSingleQuery<DB>>
= note: required because of the requirements on the impl of `diesel::query_builder::QueryFragment<_>` for `diesel::query_builder::InsertStatement<Table, <&NewModel as diesel::Insertable<Table>>::Values, diesel::query_builder::insert_statement::Insert, diesel::query_builder::returning_clause::ReturningClause<<Table as diesel::Table>::AllColumns>>`
= note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<_, _>` for `diesel::query_builder::InsertStatement<Table, <&NewModel as diesel::Insertable<Table>>::Values>`
error[E0277]: the trait bound `<Table as diesel::Table>::AllColumns: diesel::query_builder::QueryFragment<_>` is not satisfied
--> campsched_web/src/api.rs:97:45
|
97 | insert_into(table).values(new_item).get_result(db)
| ^^^^^^^^^^ the trait `diesel::query_builder::QueryFragment<_>` is not implemented for `<Table as diesel::Table>::AllColumns`
|
= help: the following implementations were found:
<&'a T as diesel::query_builder::QueryFragment<DB>>
= note: required because of the requirements on the impl of `diesel::query_builder::QueryFragment<_>` for `diesel::query_builder::returning_clause::ReturningClause<<Table as diesel::Table>::AllColumns>`
= note: required because of the requirements on the impl of `diesel::query_builder::QueryFragment<_>` for `diesel::query_builder::InsertStatement<Table, <&NewModel as diesel::Insertable<Table>>::Values, diesel::query_builder::insert_statement::Insert, diesel::query_builder::returning_clause::ReturningClause<<Table as diesel::Table>::AllColumns>>`
= note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<_, _>` for `diesel::query_builder::InsertStatement<Table, <&NewModel as diesel::Insertable<Table>>::Values>`
error[E0277]: the trait bound `diesel::pg::Pg: diesel::sql_types::HasSqlType<<<Table as diesel::Table>::AllColumns as diesel::Expression>::SqlType>` is not satisfied
--> campsched_web/src/api.rs:97:45
|
97 | insert_into(table).values(new_item).get_result(db)
| ^^^^^^^^^^ the trait `diesel::sql_types::HasSqlType<<<Table as diesel::Table>::AllColumns as diesel::Expression>::SqlType>` is not implemented for `diesel::pg::Pg`
|
= help: consider adding a `where diesel::pg::Pg: diesel::sql_types::HasSqlType<<<Table as diesel::Table>::AllColumns as diesel::Expression>::SqlType>` bound
= note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<diesel::PgConnection, _>` for `diesel::query_builder::InsertStatement<Table, <&NewModel as diesel::Insertable<Table>>::Values>`
error[E0277]: the trait bound `Model: diesel::Queryable<<<Table as diesel::Table>::AllColumns as diesel::Expression>::SqlType, diesel::pg::Pg>` is not satisfied
--> campsched_web/src/api.rs:97:45
|
97 | insert_into(table).values(new_item).get_result(db)
| ^^^^^^^^^^ the trait `diesel::Queryable<<<Table as diesel::Table>::AllColumns as diesel::Expression>::SqlType, diesel::pg::Pg>` is not implemented for `Model`
|
= help: consider adding a `where Model: diesel::Queryable<<<Table as diesel::Table>::AllColumns as diesel::Expression>::SqlType, diesel::pg::Pg>` bound
= note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<diesel::PgConnection, Model>` for `diesel::query_builder::InsertStatement<Table, <&NewModel as diesel::Insertable<Table>>::Values>`
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0277`.
error: Could not compile `campsched_web`.
To learn more, run the command again with --verbose.
@solidiquis
Copy link

Lol going to test my luck but I'm going through this same problem.. did you figure out how to do this?

@huin
Copy link
Author

huin commented Aug 26, 2022

It's long enough ago that I don't recall. I suspect that I didn't, and might have written specific cases. And then moved on to other projects. Part of the problem might have been just my being relatively inexperienced in Rust.

That said, I wouldn't be surprised if Diesel's API has moved on a bit in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment