Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Dec 15, 2023
1 parent 7882039 commit 267f586
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions ensemble/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,16 @@ impl Builder {
.await
.map_err(|e| Error::Database(e.to_string()))?;

values.first().and_then(Value::as_u64).ok_or_else(|| {
Error::Serialization(rbs::value::ext::Error::Syntax(
"Failed to parse count value".to_string(),
))
})
values
.first()
.and_then(|m| m.as_map())
.and_then(|m| m.first())
.and_then(|(_, v)| v.as_u64())
.ok_or_else(|| {
Error::Serialization(rbs::value::ext::Error::Syntax(
"Failed to parse count value".to_string(),
))
})
}

/// Execute the query and return the first result.
Expand Down Expand Up @@ -392,7 +397,7 @@ impl Builder {
pub async fn insert<Id: for<'de> serde::Deserialize<'de>, T: Into<Columns> + Send>(
&self,
columns: T,
) -> Result<Id, Error> {
) -> Result<Option<Id>, Error> {
if self.limit.is_some()
|| !self.join.is_empty()
|| !self.order.is_empty()
Expand Down Expand Up @@ -421,7 +426,7 @@ impl Builder {
.await
.map_err(|e| Error::Database(e.to_string()))?;

Ok(rbs::from_value(result.last_insert_id)?)
Ok(rbs::from_value(result.last_insert_id).ok())
}

/// Increment a column's value by a given amount. Returns the number of affected rows.
Expand Down
4 changes: 2 additions & 2 deletions ensemble_derive/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,13 @@ fn impl_create(name: &Ident, fields: &Fields, primary_key: &Field) -> TokenStrea
{
let primary_key = &primary_key.ident;
quote! {
self.#primary_key = Self::query().insert(::ensemble::value::for_db(&self)?).await?;
self.#primary_key = Self::query().insert(::ensemble::value::for_db(&self)?).await?.ok_or(::ensemble::Error::Database("failed to retrieve primary key".to_string()))?;

Ok(self)
}
} else {
quote! {
Self::query().insert(::ensemble::value::for_db(&self)?).await?;
Self::query().insert::<Self::PrimaryKey, _>(::ensemble::value::for_db(&self)?).await?;

Ok(self)
}
Expand Down

0 comments on commit 267f586

Please sign in to comment.