-
-
Notifications
You must be signed in to change notification settings - Fork 551
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
how to update dynamicly from json value #346
Comments
Hey @qyihua, thanks for the suggestions! Are you integrating sea-orm with other web API? I'm interested in your use case, can you tell me more? I think it's common to take a Json / key-value pair from user and updating the corresponding rows in the database. |
Hi, @billy1624 it's very common to update dynamicly from json value in web api, so i need a None SeaValue. |
Wow, you got some serious use case here!! I'm thinking a mass assignment API similar to Laravel Eloquent, where you need to explicitly define what column is allowed to be mass assigned. Then, passing in a PHP array as input data. We could did similar things in sea-orm
@tyt2y3 any thoughts? |
|
We could provide two methods for mass assignment
(I just name the method randomly, we can rethink it later) |
Hello, @billy1624 @tyt2y3 struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
pub addr: Option<String>,
pub other: Option<String>,
...
}
...
let allow_change_fields = vec![Column::Name, Column::Addr];
let mut actives = table::ActiveModel::new();
actives.set(Column::Id, 1.into());
// json is from user input, dynamic
let mut json = json!{addr: "new address"};
for col in allow_change_fileds{
if let Some(json_val) = json.get(col.to_string()) {
// before
/*
let new_val = if json_val.is_null() {
col.null_value() // Value::from(None::<i32>) or Value::from(None::<String>)
} else {
json_value_to_sea_value(json_val)
};
actives.set(col, new_val);
*/
// now just one line
actives.set_from_json(col, json_val)?;
}
}
actives.update(db).await?;
... |
I'm checking the changes you made with following link It looks reasonable to me but I think we need some helper methods in
All in all, this is a good starting point & demo :)) |
Thank you in advance for your attention to this issue. |
Thank you for the contribution! I think this is pretty close. |
Thank Hi, @billy1624 Thank you for your attention . The PR work for me But in my use case, a web crawler, and the primary keys come from json. I think we also need to set primary keys come from json. // Restore primary key values
for (col, active_value) in primary_key_values {
match active_value {
ActiveValue::Unchanged(v) | ActiveValue::Set(v) => self.set(col, v),
// NotSet => self.not_set(col),
NotSet => (), // do nothing
}
} |
If it's implemented in this way the
|
To case 1, we can implement an associated function named |
Hello,
Is there any way to support dynamic update from json value?
like update from user input , and only allow update special fields
if the input json field is null, need set None value for activemodel,
so it need get None value from column:
Any better suggestion?
qyihua@0fa0f33
The text was updated successfully, but these errors were encountered: