簡體   English   中英

Rust 如果表格行存在於柴油中,如何 select?

[英]Rust how to select if table row exists in diesel?

我的目標是查詢數據庫並查找用戶是否已經存在並提供 email。查看文檔,這似乎是這樣做的方式。 唯一的區別是我想根據 email 而不是用戶名進行查詢。

我有一些這樣的代碼:

src/models.rs

use crate::schema::mailing_list;
use chrono::NaiveDateTime;
use diesel::prelude::*;
use diesel::Insertable;

#[derive(Queryable, Insertable)]
#[diesel(table_name = mailing_list)]
pub struct Subscriber {
    pub uuid: String,
    pub email: String,
    pub date_subscribed: NaiveDateTime,
    pub date_unsubscribed: Option<NaiveDateTime>,
}

這里的一些邏輯已經縮短:

src/mail.rs

use chrono::Utc;
use diesel::prelude::*;
use diesel::dsl::select;
use diesel::dsl::exists;
use rocket::http::Status;
use uuid::Uuid;

use crate::db;
use crate::models::Subscriber;
use crate::schema::mailing_list::dsl::mailing_list;

#[get("/subscribe?<email>")]
pub fn subscribe(email: String) -> Status {
(...)
    let connection = &mut db::establish_connection();
(...)
    let email_exists = select(exists(mailing_list.filter(email.eq(&email))))
        .get_result(connection);
(...)
}
  Compiling rocket-website v0.1.0 (/home/cedric/Documents/Programming/personal-website/rocket-website)             
error[E0277]: the trait bound `bool: diesel::Expression` is not satisfied        
   --> src/mail.rs:27:58                                                                                            
    |                                                     
27  |     let email_exists = select(exists(mailing_list.filter(email.eq(&email))))                                  
    |                                                   ------ ^^^^^^^^^^^^^^^^ the trait `diesel::Expression` is no
t implemented for `bool`                                                                                            
    |                                                   |                                                           
    |                                                   required by a bound introduced by this call
    |                                                                                                               
    = help: the following other types implement trait `diesel::Expression`:                                         
              &'a T                                                                                                 
              (T0, T1)                                                                                              
              (T0, T1, T2)                                                                                          
              (T0, T1, T2, T3)                                                                                      
              (T0, T1, T2, T3, T4)                                                                                  
              (T0, T1, T2, T3, T4, T5)                                                                              
              (T0, T1, T2, T3, T4, T5, T6)                                                                          
              (T0, T1, T2, T3, T4, T5, T6, T7)                                                                      
            and 80 others                                                                                           
    = note: required for `SelectStatement<FromClause<table>>` to implement `FilterDsl<bool>`
    = note: 1 redundant requirement hidden                                                                          
    = note: required for `table` to implement `FilterDsl<bool>`
note: required by a bound in `diesel::QueryDsl::filter`                                                             
   --> /home/cedric/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-2.0.3/src/query_dsl/mod.rs:619:15        
    |                                                                                                               
619 |         Self: methods::FilterDsl<Predicate>,                                                                  
    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `diesel::QueryDsl::filter`              

該錯誤比上面的代碼片段長得多,但簡而言之,它提到了第 27 行(帶有 select 的行,並且存在這些錯誤:

the trait `diesel::Expression` is not implemented for `bool`
the trait `ValidGrouping<()>` is not implemented for `bool`                                                                                                                                                                       
the trait `expression::subselect::ValidSubselect<NoFromClause>` is not implemented for
`SelectStatement<FromClause<table>, query_builder::select_clause::DefaultSelectClause<FromClause<table>>, 
query_builder::distinct_clause::NoDistinctClause, query_builder::where_cl
ause::WhereClause<bool>>`                                                                   
the trait `expression::subselect::ValidSubselect<NoFromClause>` is not implemented for
 `SelectStatement<FromClause<table>, query_builder::select_clause::DefaultSelectClause<FromClause<table>>,
 query_builder::distinct_clause::NoDistinctClause, query_builder::where_clause::WhereClause<bool>>`

此外,還有其他錯誤:

error[E0277]: the trait bound `bool: QueryId` is not satisfied
    --> src/mail.rs:28:21
     |
28   |         .get_result(connection);
     |          ---------- ^^^^^^^^^^ the trait `QueryId` is not implemented for `bool`
     |          |
     |          required by a bound introduced by this call

此行也會觸發此版本的錯誤:

the trait `QueryFragment<Mysql>` is not implemented for `bool`

過濾器 function 需要來自 diesel 的特殊類型,它來自 crate::schema 命名空間中的自動生成類型。

這應該可以解決問題:

let email_exists = select(exists(mailing_list.filter(self::schema::mailing_list::dsl::email.eq(&email))))
        .get_result::<bool>(connection);

問題是您的參數email恰好與模式email中的字段之一同名。 只需重命名參數或模式字段:

#[get("/subscribe?<email>")]
pub fn subscribe(email: String) -> Status {
    // rename the schema field
    use crate::schema::mailing_list::dsl::email as email_q;
//(...)
    let connection = &mut db::establish_connection();
//(...)
    let email_exists = select(exists(mailing_list.filter(email_q.eq(&email))))
        .get_result(connection);
//(...)
}

Rust 如何實現 diesel::Insertable<div id="text_translate"><p> 我正在嘗試在 Rust 中實現特性diesel::Insertable<table 。</p><p> 在mail.rs</p><pre> diesel::insert_into(mailing_list).values( email // This is a String ).execute(connection);</pre><p> 這是因為以下代碼導致此錯誤:</p><pre> error[E0277]: the trait bound `std::string::String: diesel::Insertable<table>` is not satisfied --> src/mail.rs:18:10 | 17 | diesel::insert_into(mailing_list).values( | ------ required by a bound introduced by this call 18 | email | ^^^^^ the trait `diesel::Insertable<table>` is not implemented for `std::string::String`</pre><p> 多看一下 Diesel 的文檔,我相信我應該讓自己成為一個派生可插入的結構</p><p>在models.rs</p><pre> #[derive(Insertable)] pub struct NewSubscriber { pub email: String }</pre><p> 但后來我得到</p><pre>but im getting this error error[E0433]: failed to resolve: use of undeclared crate or module `new_subscribers` --> src/models.rs:13:12 | 13 | pub struct NewSubscriber { | ^^^^^^^^^^^^^ use of undeclared crate or module `new_subscribers`</pre><p> 我很困惑為什么編譯器說它找不到包含我正在嘗試定義的結構的板條箱或模塊。</p></div><table></table>

[英]Rust how to implement diesel::Insertable<table>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 Rust 如何實現 diesel::Insertable<div id="text_translate"><p> 我正在嘗試在 Rust 中實現特性diesel::Insertable<table 。</p><p> 在mail.rs</p><pre> diesel::insert_into(mailing_list).values( email // This is a String ).execute(connection);</pre><p> 這是因為以下代碼導致此錯誤:</p><pre> error[E0277]: the trait bound `std::string::String: diesel::Insertable<table>` is not satisfied --> src/mail.rs:18:10 | 17 | diesel::insert_into(mailing_list).values( | ------ required by a bound introduced by this call 18 | email | ^^^^^ the trait `diesel::Insertable<table>` is not implemented for `std::string::String`</pre><p> 多看一下 Diesel 的文檔,我相信我應該讓自己成為一個派生可插入的結構</p><p>在models.rs</p><pre> #[derive(Insertable)] pub struct NewSubscriber { pub email: String }</pre><p> 但后來我得到</p><pre>but im getting this error error[E0433]: failed to resolve: use of undeclared crate or module `new_subscribers` --> src/models.rs:13:12 | 13 | pub struct NewSubscriber { | ^^^^^^^^^^^^^ use of undeclared crate or module `new_subscribers`</pre><p> 我很困惑為什么編譯器說它找不到包含我正在嘗試定義的結構的板條箱或模塊。</p></div><table></table> 連接表上的 Rust Diesel 更新 rust 柴油方法“過濾器”存在於模式表,但其特征界限不滿足? 是否可以通過 rust 中的表名生成柴油模式 如何使用銹柴油進行查詢分組 Rust柴油Postgres如何添加分頁查詢 如何在 Rust Diesel 中使用時間戳和間隔進行算術運算 如何在紅豆杉或任何前端使用生銹柴油? 如何使用生銹柴油獲得柱的平均值? rust、柴油和 mysql:如何返回擁有的記錄?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM