Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upsave_changes() doesn't save the field when it's None, but should save it (as null) #1497
Comments
This comment has been minimized.
|
I don't think |
sgrif
closed this
Jan 19, 2018
This comment has been minimized.
Boscop
commented
Jan 19, 2018
|
Thanks,
|
This comment has been minimized.
|
I do see your point. However, I think that having updates always work consistently, and keeping I disagree with your assertion that I recommend reading through #26 for more context. |
This comment has been minimized.
Boscop
commented
Jan 19, 2018
|
Thanks for the quick reply. By input you mean the recommended use-case in an API server is to deserialize the request directly into a changeset struct? I never do that, I always have separate data types for controllers (different requests act on data in different ways and not every request should be allowed to change all fields, e.g. editing a reply post should not allow changing its
|
This comment has been minimized.
This all sounds reasonable for validations at the moment. I suspect we will eventually end up with something that lives at a level close to serde that handles validations, where deserializing something that fails validation is considered a deserialization error.
I think we need more applications built before we can answer that. The intent is for them to live very close together though even if they aren't the same struct (ideally they should be able to though). This is one of the main reasons that
I'm assuming by "model struct" you mean the thing that implements |
This comment has been minimized.
Boscop
commented
Jan 19, 2018
•
|
Hm, shouldn't model structs be both I never used any other ORMs before diesel, I just learned how to use diesel about a year ago by looking at this project.. (But I knew some sql/postgres before.) #[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[belongs_to(Place)]
#[belongs_to(Customer)]
#[table_name = "clients"]
pub struct Client {
pub id: i64,
pub customer_id: Option<i64>,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
pub place_id: Option<i64>,
pub unique_id: String,
pub version: i32,
pub last_seen: NaiveDateTime,
}
#[derive(Insertable)]
#[table_name = "clients"]
pub struct NewClient<'a> {
pub unique_id: &'a str,
pub version: i32,
}(And then I call e.g. Btw, please don't deprecate |
This comment has been minimized.
Yup, and if all your structs are one-to-one with database tables, that's what it'll look like. We decouple those concepts by design. Same with In Rust specifically for example, if you are building a web app, the id does not come from the same place as the rest of your input (it comes from the URL not the request body). I intend most usage to look like You don't have to follow these patterns, but they are separated for a reason.
I'm not currently planning on it. |
Boscop commentedJan 19, 2018
save_changes()doesn't save the field when it's None, only when it'sSome(val).That's very counter-intuitive. It should save all the fields!
So when the field is None, it should save it (as null in sql).
It should behave differently than an Option field in a normal changeset struct (used with normal
updatequeries), because a normal struct has different expected semantics (and has to have them, to signify not changing a field).BUT: With
save_changes()you expect ALL changes to be saved. And when you don't want to change a field, you just don't modify the struct before callingsave_changes().