我正在使用 rustdiesrustc 1.59.0 (9d1b2106e 2022-02-23)將diesel = { version = "1.4.8", features = ["postgres","64-column-tables","chrono","serde_json"] }記錄插入 PostgreSQL 13 資料庫,我想做的是在插入操作之后獲取插入的記錄 id。這是 rust 代碼的一部分,現在看起來像這樣:
let menu_id = diesel::insert_into(crate::model::diesel::dolphin::dolphin_schema::menu_resource::table)
.values(&new_menu_resource)
.returning(crate::model::diesel::dolphin::dolphin_schema::menu_resource::id)
.on_conflict_do_nothing()
.execute(&connection)
.unwrap();
這是我menu_resource在 rust 中的定義:
table! {
menu_resource (id) {
id -> Int4,
name -> Varchar,
res_type -> Int4,
created_time -> Int8,
updated_time -> Int8,
remark -> Nullable<Varchar>,
path -> Varchar,
parent_id -> Int4,
component -> Nullable<Varchar>,
sort -> Int4,
name_zh -> Varchar,
tree_id_path -> Varchar,
code -> Varchar,
}
}
但收到的menu_id是usize資料型別。為什么回傳unsize資料型別不是i32?我錯過了什么?我應該怎么做才能讓它回傳 id i32?我從這里關注檔案:https ://docs.diesel.rs/diesel/fn.insert_into.html 告訴它可以回傳這樣的值:
let inserted_names = diesel::insert_into(users)
.values(&vec![
name.eq("Diva Plavalaguna"),
name.eq("Father Vito Cornelius"),
])
.returning(name)
.get_results(&connection);
assert_eq!(Ok(vec!["Diva Plavalaguna".to_string(), "Father Vito Cornelius".to_string()]), inserted_names);
uj5u.com熱心網友回復:
的檔案RunQueryDsl::execute表明回傳值是受影響的行數,這自然用無符號型別表示usize。如果您真的想要i32,請自行轉換回傳值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/483036.html
標籤:PostgreSQL 锈 锈柴油
下一篇:SQL對表列進行計數
