diff --git a/src/database/proxy.rs b/src/database/proxy.rs index f2cacb252..0d069c658 100644 --- a/src/database/proxy.rs +++ b/src/database/proxy.rs @@ -136,7 +136,7 @@ impl From for ProxyExecResult { #[cfg(feature = "sqlx-mysql")] ExecResultHolder::SqlxMySql(result) => { ProxyExecResult::Inserted(vec![ProxyExecResultIdType::Integer( - result.last_insert_id().unwrap_or(0), + result.last_insert_id(), )]) } #[cfg(feature = "sqlx-postgres")] diff --git a/src/executor/insert.rs b/src/executor/insert.rs index 2f6aa74ce..bfd0692a6 100644 --- a/src/executor/insert.rs +++ b/src/executor/insert.rs @@ -1,7 +1,7 @@ use crate::{ - error::*, ActiveModelTrait, ColumnTrait, ConnectionTrait, EntityTrait, Insert, IntoActiveModel, - Iterable, PrimaryKeyToColumn, PrimaryKeyTrait, SelectModel, SelectorRaw, Statement, TryFromU64, - TryInsert, + error::*, ActiveModelTrait, ColumnTrait, ConnectionTrait, EntityTrait, ExecResultHolder, + Insert, IntoActiveModel, Iterable, PrimaryKeyToColumn, PrimaryKeyTrait, ProxyExecResult, + ProxyExecResultIdType, SelectModel, SelectorRaw, Statement, TryFromU64, TryInsert, }; use sea_query::{Expr, FromValueTuple, Iden, InsertStatement, IntoColumnRef, Query, ValueTuple}; use std::{future::Future, marker::PhantomData}; @@ -241,8 +241,41 @@ where if res.rows_affected() == 0 { return Err(DbErr::RecordNotInserted); } - let last_insert_id = res.last_insert_id(); - ValueTypeOf::::try_from_u64(last_insert_id).map_err(|_| DbErr::UnpackInsertId)? + match res.result { + ExecResultHolder::Proxy(val) => match val { + ProxyExecResult::Empty | ProxyExecResult::Conflicted => { + return Err(DbErr::RecordNotInserted); + } + ProxyExecResult::Inserted(val) => { + match val.last().ok_or(DbErr::UnpackInsertId)? { + ProxyExecResultIdType::Integer(val) => { + ValueTypeOf::::try_from_u64(*val) + .map_err(|_| DbErr::UnpackInsertId)? + } + ProxyExecResultIdType::String(val) => { + ValueTypeOf::::from_value_tuple(ValueTuple::One( + sea_query::Value::String(Some(Box::new(val.to_owned()))), + )) + } + ProxyExecResultIdType::Uuid(val) => { + ValueTypeOf::::from_value_tuple(ValueTuple::One( + sea_query::Value::Uuid(Some(Box::new(val.to_owned()))), + )) + } + ProxyExecResultIdType::Bytes(val) => { + ValueTypeOf::::from_value_tuple(ValueTuple::One( + sea_query::Value::Bytes(Some(Box::new(val.to_owned()))), + )) + } + } + } + }, + _ => { + let last_insert_id = res.last_insert_id(); + ValueTypeOf::::try_from_u64(last_insert_id) + .map_err(|_| DbErr::UnpackInsertId)? + } + } } };