-
-
Notifications
You must be signed in to change notification settings - Fork 541
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
Convert from ActiveModel to Model #606
Comments
Just my 2cts but, assuming you know that 1. Value is set and 2. Value is of type Uuid, wouldn't it be possible to use For example, (going to use sea_orm::ActiveValue;
use uuid::Uuid;
pub fn test() {
let test_uuids = vec![
ActiveValue::set(Uuid::new_v4()),
ActiveValue::set(Uuid::new_v4()),
ActiveValue::set(Uuid::new_v4()),
ActiveValue::set(Uuid::new_v4()),
ActiveValue::set(Uuid::new_v4()),
];
test_uuids
.iter() // Iter<ActiveValue<…>>
.cloned() // impl Iter<Item = ActiveValue<…>>
// unpack uuids
.map(|active_value| {
active_value // ActiveValue<…>
.into_value() // Option<Value>
.unwrap() // Value
.as_ref_uuid() // Option<&Uuid>
.unwrap() // &Uuid
.clone() // Uuid
}) // impl Iter<Item = Uuid>
.for_each(|uuid| {
// test if uuids are usable
println!("Uuid: {uuid}");
});
} |
I think there are two problems:
|
What is the difference between |
Hey @kirawi, https://www.sea-ql.org/SeaORM/docs/basic-crud/insert#model--activemodel |
Hi @billy1624 , I'm a new comer and would like to work on this 'good first issue'. I'd appreciate it if you could give me some instructions. |
Hey @greenhandatsjtu, thanks for reaching out! I think we can starts small, and take on the first task...
We could introduce a new trait, pub trait TryIntoModel<M>
where
A: ModelTrait,
{
fn try_into_model(self) -> Result<M, DbErr>;
} Then, we implement it inside derive macros of active model. Again, similar to what we did for converting impl std::convert::TryFrom<ActiveModel> for <Entity as EntityTrait>::Model { ... }
impl sea_orm::TryIntoModel<<Entity as EntityTrait>::Model> for ActiveModel { ... } |
@billy1624 thanks for your advice, now I know where to start. I will try to work on the first task, but I'm not sure I can make it, sorry if I bother you again when I meet problems that I can't solve. Or should I make a draft PR if I've made some progess, so you can point out where's wrong or what to do next in that PR, not in this issue anymore? |
Hey @greenhandatsjtu, it's perfectly okay to ping me :D |
Summary
Right now it is impossible to convert from
ActiveModel -> Model
or fromActiveValue<T> -> T
?Motivation
When using
insert_many(..)
the return type is useless. ( I assume this is to support MySQL which does not allowreturning
clause)So I need to get the models back by making another round-trip to the database (not the end of the world) or converting the
ActiveModel
's.However, from my
Vec<ActiveModel>
, I cannot find a way to go toVec<Model>
or evenVec<Uuid>
.This makes it very hard to get the result of a multi-insert.
The text was updated successfully, but these errors were encountered: