tedtanner
tedtanner

Reputation: 695

Rust Diesel update on joined table

I am trying to update a field in a table use Diesel. I need to verify an association in another table, so I am LEFT JOIN-ing the other table. Here is a SQL representation of what I am doing:

UPDATE table_name AS tn
SET field = 'value'
FROM table_name
LEFT JOIN other_table AS ot
    ON ot.id_field_one = {some_id}
WHERE tn.id_field_one = {some_id} and ot.id_field_two = {some_different_id};

Here is what I have tried with Diesel:

dsl::update(
    table_name
        .left_join(other_table.on(other_table_fields::id_field_one.eq(some_id)))
        .filter(table_fields::id_field_one.eq(some_id))
        .filter(other_table_fields::id_field_two.eq(some_different_id))
)
.set(table_fields::field.eq("value"))
.execute(db_connection)?;

I get the following error (I've replaced the table and field names to match the names of my examples above):

error[E0277]: the trait bound `diesel::query_builder::SelectStatement<JoinOn<diesel::query_source::joins::Join<table_name::table, other_table::table, LeftOuter>, diesel::expression::operators::Eq<other_table::columns::some_id, diesel::expression::bound::Bound<diesel::sql_types::Uuid, uuid::Uuid>>>, query_builder::select_clause::DefaultSelectClause, query_builder::distinct_clause::NoDistinctClause, query_builder::where_clause::WhereClause<And<diesel::expression::operators::Eq<other_table::columns::some_different_id, diesel::expression::bound::Bound<diesel::sql_types::Uuid, uuid::Uuid>>, diesel::expression::operators::Eq<table_name::columns::id, diesel::expression::bound::Bound<diesel::sql_types::Uuid, uuid::Uuid>>>>>: diesel::query_builder::IntoUpdateTarget` is not satisfied
   --> src/file.rs:297:9
    |
296 |       match dsl::update(
    |             ----------- required by a bound introduced by this call
297 | /         table_name
298 | |             .left_join(other_table.on(other_table_fields::some_id.eq(some_id)))
299 | |             .filter(other_table_fields::some_different_id.eq(some_different_id))
300 | |             .filter(table_fields::id.eq(some_id)),
    | |________________________________________________________________^ the trait `diesel::query_builder::IntoUpdateTarget` is not implemented for `diesel::query_builder::SelectStatement<JoinOn<diesel::query_source::joins::Join<table_name::table, other_table::table, LeftOuter>, diesel::expression::operators::Eq<other_table::columns::some_id, diesel::expression::bound::Bound<diesel::sql_types::Uuid, uuid::Uuid>>>, query_builder::select_clause::DefaultSelectClause, query_builder::distinct_clause::NoDistinctClause, query_builder::where_clause::WhereClause<And<diesel::expression::operators::Eq<other_table::columns::some_different_id, diesel::expression::bound::Bound<diesel::sql_types::Uuid, uuid::Uuid>>, diesel::expression::operators::Eq<table_name::columns::id, diesel::expression::bound::Bound<diesel::sql_types::Uuid, uuid::Uuid>>>>>`
    |
    = help: the trait `diesel::query_builder::IntoUpdateTarget` is implemented for `diesel::query_builder::SelectStatement<F, query_builder::select_clause::DefaultSelectClause, query_builder::distinct_clause::NoDistinctClause, W>`
note: required by a bound in `diesel::update`
   --> /Users/tanner/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.4.8/src/query_builder/functions.rs:80:18
    |
80  | pub fn update<T: IntoUpdateTarget>(source: T) -> UpdateStatement<T::Table, T::WhereClause> {
    |                  ^^^^^^^^^^^^^^^^ required by this bound in `diesel::update`

error[E0277]: the trait bound `JoinOn<diesel::query_source::joins::Join<table_name::table, other_table::table, LeftOuter>, diesel::expression::operators::Eq<other_table::columns::some_id, diesel::expression::bound::Bound<diesel::sql_types::Uuid, uuid::Uuid>>>: HasTable` is not satisfied

The SQL works when I run it as a query, but I can't figure out how to represent the same thing in working Rust code. What am I missing here?

Upvotes: 2

Views: 582

Answers (1)

weiznich
weiznich

Reputation: 3455

Diesel does currently not support joins in that position. You can either use diesel::sql_query to write the whole query or provide a custom query dsl extension for your specific query.

Upvotes: 2

Related Questions