Refactor ActiveModel insert logic
This commit is contained in:
parent
c3084e425f
commit
c0ee423f0a
@ -74,15 +74,14 @@ pub trait ActiveModelTrait: Clone + Debug {
|
|||||||
let am = self;
|
let am = self;
|
||||||
let exec = <Self::Entity as EntityTrait>::insert(am).exec(db);
|
let exec = <Self::Entity as EntityTrait>::insert(am).exec(db);
|
||||||
let res = exec.await?;
|
let res = exec.await?;
|
||||||
// TODO: if the entity does not have auto increment primary key, then last_insert_id is a wrong value
|
// Assume valid last_insert_id is not equals to Default::default()
|
||||||
// FIXME: Assumed valid last_insert_id is not equals to Default::default()
|
if res.last_insert_id
|
||||||
if <<Self::Entity as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::auto_increment()
|
!= <<Self::Entity as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::ValueType::default()
|
||||||
&& res.last_insert_id != <<Self::Entity as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::ValueType::default()
|
|
||||||
{
|
{
|
||||||
let find = <Self::Entity as EntityTrait>::find_by_id(res.last_insert_id).one(db);
|
let found = <Self::Entity as EntityTrait>::find_by_id(res.last_insert_id)
|
||||||
let found = find.await;
|
.one(db)
|
||||||
let model: Option<<Self::Entity as EntityTrait>::Model> = found?;
|
.await?;
|
||||||
match model {
|
match found {
|
||||||
Some(model) => Ok(model.into_active_model()),
|
Some(model) => Ok(model.into_active_model()),
|
||||||
None => Err(DbErr::Exec("Failed to find inserted item".to_owned())),
|
None => Err(DbErr::Exec("Failed to find inserted item".to_owned())),
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user