What's the difference between unchanged and unset? #770
-
Hi, I'm new to SeaORM and feels a bit confused when dealing with I don't quite get the point of when should I use ActiveValue::Unchanged or ActiveValue::NotSet I made a small experiment and the returned sql seems to be the same. Updateassert_eq!(
Update::one(fruit::ActiveModel {
id: ActiveValue::set(1),
name: ActiveValue::set("Orange".to_owned()),
cake_id: ActiveValue::not_set(),
})
.build(DbBackend::Postgres)
.to_string(),
Update::one(fruit::ActiveModel {
id: ActiveValue::set(1),
name: ActiveValue::set("Orange".to_owned()),
cake_id: ActiveValue::unchanged(Some(12345)),
})
.build(DbBackend::Postgres)
.to_string(),
); Both queries returns Insertassert_eq!(
Insert::one(fruit::ActiveModel {
id: ActiveValue::set(1),
name: ActiveValue::set("Orange".to_owned()),
cake_id: ActiveValue::set(Some(12345)),
})
.build(DbBackend::Postgres)
.to_string(),
Insert::one(fruit::ActiveModel {
id: ActiveValue::set(1),
name: ActiveValue::set("Orange".to_owned()),
cake_id: ActiveValue::unchanged(Some(12345)),
})
.build(DbBackend::Postgres)
.to_string(),
); Both queries returns It seems to me that unchanged is completely replaceable, and it makes me confused when to use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @saintazunya, welcome and thanks for the supports! Generally speaking you don't need For update, what you want is just update the changed / setted values. That's why the unchanged value is being ignored. For insert, we want all setted values including those unchanged values to be inserted into db. That's the behaviour for update & insert :) |
Beta Was this translation helpful? Give feedback.
Hey @saintazunya, welcome and thanks for the supports!
Generally speaking you don't need
ActiveValue::unchanged( ... )
. All you need is justset()
andnot_set()
.For update, what you want is just update the changed / setted values. That's why the unchanged value is being ignored.
For insert, we want all setted values including those unchanged values to be inserted into db.
That's the behaviour for update & insert :)