【发布时间】:2022-12-10 10:19:31
【问题描述】:
是否可以使用柴油创建更通用的查询? 我知道如何为每个属性创建更新或删除。但我的观点是为许多结构创建通用更新。 我的模特:
#[derive(Associations, Identifiable, Queryable, PartialEq, Debug)]
#[diesel(belongs_to(User))]
#[diesel(table_name = books)]
pub struct Book {
pub id: i32,
pub user_id: i32,
pub title: String,
pub body: String,
pub book_description: String,
pub book_image: Option<String>,
pub publish_day: chrono::NaiveDateTime,
}
#[derive(Associations, Identifiable, Queryable, PartialEq, Debug)]
#[diesel(belongs_to(Book))]
#[diesel(table_name = book_comments)]
pub struct BookComment {
pub id: i32,
pub book_id: i32,
pub body: String,
pub publish_day: chrono::NaiveDateTime,
}
我的模式:
diesel::table! {
books (id) {
id -> Int4,
user_id -> Int4,
title -> Varchar,
body -> Text,
book_description -> Varchar,
book_image -> Nullable<Varchar>,
publish_day -> Timestamp,
}
}
diesel::table! {
book_comments (id) {
id -> Int4,
book_id -> Int4,
body -> Varchar,
publish_day -> Timestamp,
}
}
工作版本:
pub fn upate<T>(&mut self, book_id: &i32, title: &str) -> Book {
let result = diesel::update(books::table)
.filter(book::id.eq(&book_id))
.set(book::title.eq(title))
.get_result::<Book>(&mut self.connection)
.expect("Failed to update book");
result
}
我正在尝试这样的事情:
pub fn upate<T, U, P: Table, S>(&mut self, find_by: &T, change: U, target_table: Table,change_param: &S) -> Book {
let result = diesel::update(target_table)
.filter(find_by.eq(&book_id))
.set(S.eq(change))
.get_result::<U>(&mut self.connection)
.expect("Failed to update book");
result
但是当然不行。这可能吗?
对解决方案帮助不大。
【问题讨论】:
-
这回答了你的问题了吗? Diesel generic update
标签: postgresql rust orm rust-diesel