Rust diesel 有条件地更新字段
Rust diesel conditionally update fields
我正在尝试为一个项目使用 diesel diesel = { version = "1.4.8", features = ["postgres","64-column-tables","chrono","serde_json"] }
,我想更新 postgresql table 中的一些字段,来自客户端的传递参数定义如下:
pub struct UpdateChannelRequest {
pub channelId: i64,
pub tags: Option<Vec<Tag>>,
pub commentRss: i32,
pub partOutput: i32,
pub subStatus: i32
}
我现在正在使用此代码更新 table:
pub fn update_channel_impl(request: &Json<UpdateChannelRequest>){
use crate::model::diesel::dolphin::dolphin_schema::rss_sub_source::dsl::*;
let connection = config::establish_connection();
let predicate = crate::model::diesel::dolphin::dolphin_schema::rss_sub_source::id.eq(request.channelId);
diesel::update(rss_sub_source.filter(predicate))
.set((comment_rss.eq(&request.commentRss),part_output.eq(&request.partOutput),sub_status.eq(&request.subStatus)))
.get_result::<RssSubSource>(&connection)
.expect("unable to update channel");
}
我面临的问题是前端每次都需要传递所有更新字段。是否可以通过传递字段进行服务器端条件更新?例如,定义一些字段为Option,如果传递了字段值,更新它。如果不传值,直接忽略字段(必须传id,因为我们需要通过id更新记录)。
If the struct has any optional fields on it, these will also have special behavior. By default, #[derive(AsChangeset)] will assume that None means that you don’t wish to assign that field.
我正在尝试为一个项目使用 diesel diesel = { version = "1.4.8", features = ["postgres","64-column-tables","chrono","serde_json"] }
,我想更新 postgresql table 中的一些字段,来自客户端的传递参数定义如下:
pub struct UpdateChannelRequest {
pub channelId: i64,
pub tags: Option<Vec<Tag>>,
pub commentRss: i32,
pub partOutput: i32,
pub subStatus: i32
}
我现在正在使用此代码更新 table:
pub fn update_channel_impl(request: &Json<UpdateChannelRequest>){
use crate::model::diesel::dolphin::dolphin_schema::rss_sub_source::dsl::*;
let connection = config::establish_connection();
let predicate = crate::model::diesel::dolphin::dolphin_schema::rss_sub_source::id.eq(request.channelId);
diesel::update(rss_sub_source.filter(predicate))
.set((comment_rss.eq(&request.commentRss),part_output.eq(&request.partOutput),sub_status.eq(&request.subStatus)))
.get_result::<RssSubSource>(&connection)
.expect("unable to update channel");
}
我面临的问题是前端每次都需要传递所有更新字段。是否可以通过传递字段进行服务器端条件更新?例如,定义一些字段为Option,如果传递了字段值,更新它。如果不传值,直接忽略字段(必须传id,因为我们需要通过id更新记录)。
If the struct has any optional fields on it, these will also have special behavior. By default, #[derive(AsChangeset)] will assume that None means that you don’t wish to assign that field.