Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG:update_by_wrapper update非数据库column字段 #91

Closed
JinAirsOs opened this issue Apr 8, 2021 · 9 comments
Closed

BUG:update_by_wrapper update非数据库column字段 #91

JinAirsOs opened this issue Apr 8, 2021 · 9 comments

Comments

@JinAirsOs
Copy link

我建立了一个struct和trait如下:

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct IpInfo {
    pub id: Option<u32>,
    pub net_group_id: Option<u32>,
    pub ip_pool_id: Option<u32>,
    pub ip: Option<String>,
    pub mask: Option<String>,
    pub gateway: Option<String>,
    pub status: Option<u8>, //0-> unused 1-> using 2-> reserved 3->abandoned
    pub pod: Option<String>,
    pub create_time: Option<NaiveDateTime>,

    //none table column
    pub ip_pool: Option<String>
}

impl CRUDTable for IpInfo {
    type IdType = u32; //默认提供IdType类型即可,接口里其他的method默认使用json序列化实现
    fn get_id(&self) -> Option<&Self::IdType> {
        return self.id.as_ref();
    } // 必须实现获取id值
    //fn table_name() -> String {} //可重写,默认ip_pool
    fn table_columns() -> String {
        "id,net_group_id,ip_pool_id,ip,mask,gateway,status,pod,create_time".to_string()
    } //可重写
    //fn format_chain() -> Vec<Box<dyn ColumnFormat>>{} //可重写
}

其中ip_pool字段不是数据库字段,只是后续给前端返回需要填上去的。所以我在impl CRUDTable里声明了table_columns(),把它去掉了,增删查没什么问题,但是
db.update_by_wrapper::<IpInfo>("", &mut update_ip, &w, true)的时候,发现log里面生成的sql,如下:

2021-04-08 02:48:40.399983300 +00:00 INFO rbatis::plugin::log - [rbatis] [] Exec  ==> update ip_info set  create_time = ?, gateway = ?, ip = ?, ip_pool = ?, ip_pool_id = ?, mask = ?, net_group_id = ?, pod = ?, status = ?  where id = ?
                                                                [rbatis] [] Args  ==> ["2021-04-07T12:38:50","10.85.243.1","10.85.243.20",null,3,"255.255.255.0",3,null,0,277]
thread 'actix-rt:worker:1' panicked at 'called `Result::unwrap()` on an `Err` value: E("error returned from database: 1054 (42S22): Unknown column \'ip_pool\' in \'field list\'")', src/ip_manage.rs:125:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

居然update了ip_pool,这个非数据库字段,导致报错。。。
我排查了一下。发现这个方法是直接json!打包然后更新,并未读取table_columns()然后做更新,这是个问题。盼及时修复!,我在生产使用,太难了。。。

@JinAirsOs
Copy link
Author

方便临时提供一下work around的办法,毕竟这是一个bug。不能等你们修复我再修复。

@zhuxiujia
Copy link
Member

zhuxiujia commented Apr 8, 2021

我建立了一个struct和trait如下:

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct IpInfo {
    pub id: Option<u32>,
    pub net_group_id: Option<u32>,
    pub ip_pool_id: Option<u32>,
    pub ip: Option<String>,
    pub mask: Option<String>,
    pub gateway: Option<String>,
    pub status: Option<u8>, //0-> unused 1-> using 2-> reserved 3->abandoned
    pub pod: Option<String>,
    pub create_time: Option<NaiveDateTime>,

    //none table column
    pub ip_pool: Option<String>
}

impl CRUDTable for IpInfo {
    type IdType = u32; //默认提供IdType类型即可,接口里其他的method默认使用json序列化实现
    fn get_id(&self) -> Option<&Self::IdType> {
        return self.id.as_ref();
    } // 必须实现获取id值
    //fn table_name() -> String {} //可重写,默认ip_pool
    fn table_columns() -> String {
        "id,net_group_id,ip_pool_id,ip,mask,gateway,status,pod,create_time".to_string()
    } //可重写
    //fn format_chain() -> Vec<Box<dyn ColumnFormat>>{} //可重写
}

其中ip_pool字段不是数据库字段,只是后续给前端返回需要填上去的。所以我在impl CRUDTable里声明了table_columns(),把它去掉了,增删查没什么问题,但是
db.update_by_wrapper::<IpInfo>("", &mut update_ip, &w, true)的时候,发现log里面生成的sql,如下:

2021-04-08 02:48:40.399983300 +00:00 INFO rbatis::plugin::log - [rbatis] [] Exec  ==> update ip_info set  create_time = ?, gateway = ?, ip = ?, ip_pool = ?, ip_pool_id = ?, mask = ?, net_group_id = ?, pod = ?, status = ?  where id = ?
                                                                [rbatis] [] Args  ==> ["2021-04-07T12:38:50","10.85.243.1","10.85.243.20",null,3,"255.255.255.0",3,null,0,277]
thread 'actix-rt:worker:1' panicked at 'called `Result::unwrap()` on an `Err` value: E("error returned from database: 1054 (42S22): Unknown column \'ip_pool\' in \'field list\'")', src/ip_manage.rs:125:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

居然update了ip_pool,这个非数据库字段,导致报错。。。
我排查了一下。发现这个方法是直接json!打包然后更新,并未读取table_columns()然后做更新,这是个问题。盼及时修复!,我在生产使用,太难了。。。

你的表结构请使用

 #[crud_enable( id_name:"id" |  id_type:"u32" | table_name:"ip_info" | table_columns:"id,net_group_id,ip_pool_id,ip,mask,gateway,status,pod,create_time")]
    #[derive(Clone, Debug)]
    pub struct IpInfo {
     pub id: Option<u32>,
     pub net_group_id: Option<u32>,
     pub ip_pool_id: Option<u32>,
     pub ip: Option<String>,
     pub mask: Option<String>,
     pub gateway: Option<String>,
     pub status: Option<u8>, //0-> unused 1-> using 2-> reserved 3->abandoned
     pub pod: Option<String>,
     pub create_time: Option<NaiveDateTime>,
 
     //none table column
     pub ip_pool: Option<String>
    }

@JinAirsOs
Copy link
Author

我建立了一个struct和trait如下:

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct IpInfo {
    pub id: Option<u32>,
    pub net_group_id: Option<u32>,
    pub ip_pool_id: Option<u32>,
    pub ip: Option<String>,
    pub mask: Option<String>,
    pub gateway: Option<String>,
    pub status: Option<u8>, //0-> unused 1-> using 2-> reserved 3->abandoned
    pub pod: Option<String>,
    pub create_time: Option<NaiveDateTime>,

    //none table column
    pub ip_pool: Option<String>
}

impl CRUDTable for IpInfo {
    type IdType = u32; //默认提供IdType类型即可,接口里其他的method默认使用json序列化实现
    fn get_id(&self) -> Option<&Self::IdType> {
        return self.id.as_ref();
    } // 必须实现获取id值
    //fn table_name() -> String {} //可重写,默认ip_pool
    fn table_columns() -> String {
        "id,net_group_id,ip_pool_id,ip,mask,gateway,status,pod,create_time".to_string()
    } //可重写
    //fn format_chain() -> Vec<Box<dyn ColumnFormat>>{} //可重写
}

其中ip_pool字段不是数据库字段,只是后续给前端返回需要填上去的。所以我在impl CRUDTable里声明了table_columns(),把它去掉了,增删查没什么问题,但是
db.update_by_wrapper::<IpInfo>("", &mut update_ip, &w, true)的时候,发现log里面生成的sql,如下:

2021-04-08 02:48:40.399983300 +00:00 INFO rbatis::plugin::log - [rbatis] [] Exec  ==> update ip_info set  create_time = ?, gateway = ?, ip = ?, ip_pool = ?, ip_pool_id = ?, mask = ?, net_group_id = ?, pod = ?, status = ?  where id = ?
                                                                [rbatis] [] Args  ==> ["2021-04-07T12:38:50","10.85.243.1","10.85.243.20",null,3,"255.255.255.0",3,null,0,277]
thread 'actix-rt:worker:1' panicked at 'called `Result::unwrap()` on an `Err` value: E("error returned from database: 1054 (42S22): Unknown column \'ip_pool\' in \'field list\'")', src/ip_manage.rs:125:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

居然update了ip_pool,这个非数据库字段,导致报错。。。
我排查了一下。发现这个方法是直接json!打包然后更新,并未读取table_columns()然后做更新,这是个问题。盼及时修复!,我在生产使用,太难了。。。

你的表结构请使用

 #[crud_enable( id_name:"id" |  id_type:"u32" | table_name:"ip_info" | table_columns:"id,net_group_id,ip_pool_id,ip,mask,gateway,status,pod,create_time")]
    #[derive(Clone, Debug)]
    pub struct IpInfo {
     pub id: Option<u32>,
     pub net_group_id: Option<u32>,
     pub ip_pool_id: Option<u32>,
     pub ip: Option<String>,
     pub mask: Option<String>,
     pub gateway: Option<String>,
     pub status: Option<u8>, //0-> unused 1-> using 2-> reserved 3->abandoned
     pub pod: Option<String>,
     pub create_time: Option<NaiveDateTime>,
 
     //none table column
     pub ip_pool: Option<String>
    }

我的理解是,update了非table字段,用什么方式写这个没区别,用宏还是这种写法都没法阻止update_by_wrapper没有判断是不是column字段

@JinAirsOs
Copy link
Author

我已经看了源代码 ,column字段是用json!来推断出来的,没判断这个字段,我在哪里写都是一样的。而且我还必须用这个函数,因为我要update none的字段

@zhuxiujia
Copy link
Member

我建立了一个struct和trait如下:

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct IpInfo {
    pub id: Option<u32>,
    pub net_group_id: Option<u32>,
    pub ip_pool_id: Option<u32>,
    pub ip: Option<String>,
    pub mask: Option<String>,
    pub gateway: Option<String>,
    pub status: Option<u8>, //0-> unused 1-> using 2-> reserved 3->abandoned
    pub pod: Option<String>,
    pub create_time: Option<NaiveDateTime>,

    //none table column
    pub ip_pool: Option<String>
}

impl CRUDTable for IpInfo {
    type IdType = u32; //默认提供IdType类型即可,接口里其他的method默认使用json序列化实现
    fn get_id(&self) -> Option<&Self::IdType> {
        return self.id.as_ref();
    } // 必须实现获取id值
    //fn table_name() -> String {} //可重写,默认ip_pool
    fn table_columns() -> String {
        "id,net_group_id,ip_pool_id,ip,mask,gateway,status,pod,create_time".to_string()
    } //可重写
    //fn format_chain() -> Vec<Box<dyn ColumnFormat>>{} //可重写
}

其中ip_pool字段不是数据库字段,只是后续给前端返回需要填上去的。所以我在impl CRUDTable里声明了table_columns(),把它去掉了,增删查没什么问题,但是
db.update_by_wrapper::<IpInfo>("", &mut update_ip, &w, true)的时候,发现log里面生成的sql,如下:

2021-04-08 02:48:40.399983300 +00:00 INFO rbatis::plugin::log - [rbatis] [] Exec  ==> update ip_info set  create_time = ?, gateway = ?, ip = ?, ip_pool = ?, ip_pool_id = ?, mask = ?, net_group_id = ?, pod = ?, status = ?  where id = ?
                                                                [rbatis] [] Args  ==> ["2021-04-07T12:38:50","10.85.243.1","10.85.243.20",null,3,"255.255.255.0",3,null,0,277]
thread 'actix-rt:worker:1' panicked at 'called `Result::unwrap()` on an `Err` value: E("error returned from database: 1054 (42S22): Unknown column \'ip_pool\' in \'field list\'")', src/ip_manage.rs:125:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

居然update了ip_pool,这个非数据库字段,导致报错。。。
我排查了一下。发现这个方法是直接json!打包然后更新,并未读取table_columns()然后做更新,这是个问题。盼及时修复!,我在生产使用,太难了。。。

你的表结构请使用

 #[crud_enable( id_name:"id" |  id_type:"u32" | table_name:"ip_info" | table_columns:"id,net_group_id,ip_pool_id,ip,mask,gateway,status,pod,create_time")]
    #[derive(Clone, Debug)]
    pub struct IpInfo {
     pub id: Option<u32>,
     pub net_group_id: Option<u32>,
     pub ip_pool_id: Option<u32>,
     pub ip: Option<String>,
     pub mask: Option<String>,
     pub gateway: Option<String>,
     pub status: Option<u8>, //0-> unused 1-> using 2-> reserved 3->abandoned
     pub pod: Option<String>,
     pub create_time: Option<NaiveDateTime>,
 
     //none table column
     pub ip_pool: Option<String>
    }

我的理解是,update了非table字段,用什么方式写这个没区别,用宏还是这种写法都没法阻止update_by_wrapper没有判断是不是column字段

是一个bug,临时解决的话,可以用py_sql,用sql的形式临时规避一下这个问题

@zhuxiujia
Copy link
Member

我建立了一个struct和trait如下:

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct IpInfo {
    pub id: Option<u32>,
    pub net_group_id: Option<u32>,
    pub ip_pool_id: Option<u32>,
    pub ip: Option<String>,
    pub mask: Option<String>,
    pub gateway: Option<String>,
    pub status: Option<u8>, //0-> unused 1-> using 2-> reserved 3->abandoned
    pub pod: Option<String>,
    pub create_time: Option<NaiveDateTime>,

    //none table column
    pub ip_pool: Option<String>
}

impl CRUDTable for IpInfo {
    type IdType = u32; //默认提供IdType类型即可,接口里其他的method默认使用json序列化实现
    fn get_id(&self) -> Option<&Self::IdType> {
        return self.id.as_ref();
    } // 必须实现获取id值
    //fn table_name() -> String {} //可重写,默认ip_pool
    fn table_columns() -> String {
        "id,net_group_id,ip_pool_id,ip,mask,gateway,status,pod,create_time".to_string()
    } //可重写
    //fn format_chain() -> Vec<Box<dyn ColumnFormat>>{} //可重写
}

其中ip_pool字段不是数据库字段,只是后续给前端返回需要填上去的。所以我在impl CRUDTable里声明了table_columns(),把它去掉了,增删查没什么问题,但是
db.update_by_wrapper::<IpInfo>("", &mut update_ip, &w, true)的时候,发现log里面生成的sql,如下:

2021-04-08 02:48:40.399983300 +00:00 INFO rbatis::plugin::log - [rbatis] [] Exec  ==> update ip_info set  create_time = ?, gateway = ?, ip = ?, ip_pool = ?, ip_pool_id = ?, mask = ?, net_group_id = ?, pod = ?, status = ?  where id = ?
                                                                [rbatis] [] Args  ==> ["2021-04-07T12:38:50","10.85.243.1","10.85.243.20",null,3,"255.255.255.0",3,null,0,277]
thread 'actix-rt:worker:1' panicked at 'called `Result::unwrap()` on an `Err` value: E("error returned from database: 1054 (42S22): Unknown column \'ip_pool\' in \'field list\'")', src/ip_manage.rs:125:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

居然update了ip_pool,这个非数据库字段,导致报错。。。
我排查了一下。发现这个方法是直接json!打包然后更新,并未读取table_columns()然后做更新,这是个问题。盼及时修复!,我在生产使用,太难了。。。

你的表结构请使用

 #[crud_enable( id_name:"id" |  id_type:"u32" | table_name:"ip_info" | table_columns:"id,net_group_id,ip_pool_id,ip,mask,gateway,status,pod,create_time")]
    #[derive(Clone, Debug)]
    pub struct IpInfo {
     pub id: Option<u32>,
     pub net_group_id: Option<u32>,
     pub ip_pool_id: Option<u32>,
     pub ip: Option<String>,
     pub mask: Option<String>,
     pub gateway: Option<String>,
     pub status: Option<u8>, //0-> unused 1-> using 2-> reserved 3->abandoned
     pub pod: Option<String>,
     pub create_time: Option<NaiveDateTime>,
 
     //none table column
     pub ip_pool: Option<String>
    }

我的理解是,update了非table字段,用什么方式写这个没区别,用宏还是这种写法都没法阻止update_by_wrapper没有判断是不是column字段

有一定区别的,比如table_column字段,用宏比用实现接口 速度要快很多,因为宏直接重新实现直接return 定义的值,而接口必须走序列化有一定的性能消耗

@JinAirsOs
Copy link
Author

我写了一个PR #92

@JinAirsOs
Copy link
Author

能先打个tag我试用一下么?项目比较急

zhuxiujia added a commit that referenced this issue Apr 8, 2021
@zhuxiujia
Copy link
Member

能先打个tag我试用一下么?项目比较急

已发版

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants