Rust Diesel 更新连接表

标签 rust rust-diesel

我正在尝试使用 Diesel 更新表中的字段。我需要验证另一个表中的关联,因此我LEFT JOIN-ing另一个表。这是我正在做的事情的 SQL 表示:

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};

这是我对 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)?;

我收到以下错误(我已替换表和字段名称以匹配上面示例的名称):

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

当我将 SQL 作为查询运行时,它可以工作,但我不知道如何在工作 Rust 代码中表示相同的东西。我在这里缺少什么?

最佳答案

Diesel 目前不支持该位置的连接。您可以使用diesel::sql_query 编写整个查询,也可以为您的特定查询提供自定义查询dsl 扩展。

关于Rust Diesel 更新连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73059729/

相关文章:

http - Rust/Serde/HTTP : Serialize `Option<http::Method>`

generics - 使用 Diesel 的通用函数会导致溢出

rust - 有没有办法在没有 overflowing_literals 警告的情况下通过二进制赋值获得负数?

Rust(新手): Read and write (mutable) access to the same underlying array from multiple threads for in-memory database?

struct - 如何在强制使用 "new"构造函数的同时使结构的所有字段公开可读

rust - 如何在柴油中实现嵌套过滤器?

使用rust 柴油机 : the trait bound `NaiveDateTime: Deserialize<' _>` is not satisfied

rust - Actix-web : send message to db handler, 有条件地向第二个处理程序发送消息

rust - 如何使用 Diesel 编写多个显式的 inner_join?

struct - 有没有一种优雅的方法可以在没有 PhantomData 的情况下使用未使用的类型制作通用元组结构?