Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions sea-orm-macros/src/derives/active_model_ex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote};
use syn::{Attribute, Data, LitStr, PathArguments, Type, TypePath, Visibility};

fn transaction_trait_bounds() -> TokenStream {
if cfg!(feature = "async") {
quote! {
C: sea_orm::TransactionTrait + std::marker::Sync,
<C as sea_orm::TransactionTrait>::Transaction:
sea_orm::TransactionTrait<
Transaction = <C as sea_orm::TransactionTrait>::Transaction,
> + std::marker::Send,
}
Comment thread
Huliiiiii marked this conversation as resolved.
} else {
quote!(C: sea_orm::TransactionTrait,)
}
}

enum RelationAttr {
BelongsTo {
from: RelationColumns,
Expand Down Expand Up @@ -676,6 +690,7 @@ impl ActiveModelActionTokens {
fn expand(self) -> TokenStream {
let async_ = async_token();
let await_ = await_token();
let transaction_trait_bounds = transaction_trait_bounds();
let Self {
belongs_to_action,
belongs_to_after_action,
Expand All @@ -694,7 +709,7 @@ impl ActiveModelActionTokens {
#[doc = " Generated by sea-orm-macros"]
pub #async_ fn insert<'a, C>(self, db: &'a C) -> Result<ModelEx, sea_orm::DbErr>
where
C: sea_orm::TransactionTrait,
#transaction_trait_bounds
{
let active_model = self.action(sea_orm::ActiveModelAction::Insert, db)#await_?;
active_model.try_into()
Expand All @@ -703,7 +718,7 @@ impl ActiveModelActionTokens {
#[doc = " Generated by sea-orm-macros"]
pub #async_ fn update<'a, C>(self, db: &'a C) -> Result<ModelEx, sea_orm::DbErr>
where
C: sea_orm::TransactionTrait,
#transaction_trait_bounds
{
let active_model = self.action(sea_orm::ActiveModelAction::Update, db)#await_?;
active_model.try_into()
Expand All @@ -712,15 +727,15 @@ impl ActiveModelActionTokens {
#[doc = " Generated by sea-orm-macros"]
pub #async_ fn save<'a, C>(self, db: &'a C) -> Result<Self, sea_orm::DbErr>
where
C: sea_orm::TransactionTrait,
#transaction_trait_bounds
{
self.action(sea_orm::ActiveModelAction::Save, db)#await_
}

#[doc = " Generated by sea-orm-macros"]
pub #async_ fn delete<'a, C>(self, db: &'a C) -> Result<sea_orm::DeleteResult, sea_orm::DbErr>
where
C: sea_orm::TransactionTrait,
#transaction_trait_bounds
{
use sea_orm::{IntoActiveModel, TransactionSession};

Expand All @@ -743,7 +758,7 @@ impl ActiveModelActionTokens {
#[doc = " Generated by sea-orm-macros"]
pub #async_ fn action<'a, C>(mut self, action: sea_orm::ActiveModelAction, db: &'a C) -> Result<Self, sea_orm::DbErr>
where
C: sea_orm::TransactionTrait,
#transaction_trait_bounds
{
use sea_orm::{ActiveBelongsTo, ActiveHasOne, ActiveHasMany, IntoActiveModel, TransactionSession};
let txn = db.begin()#await_?;
Expand Down Expand Up @@ -827,6 +842,7 @@ fn expand_active_model_ex<'a>(
) -> syn::Result<TokenStream> {
let async_ = async_token();
let await_ = await_token();
let transaction_trait_bounds = transaction_trait_bounds();
let active_model_trait_methods =
DeriveActiveModel::new(vis, ident, data)?.impl_active_model_trait_methods();
let Output {
Expand Down Expand Up @@ -949,7 +965,7 @@ fn expand_active_model_ex<'a>(
#[doc = " Generated by sea-orm-macros"]
pub #async_ fn cascade_delete<'a, C>(self, db: &'a C) -> Result<sea_orm::DeleteResult, sea_orm::DbErr>
where
C: sea_orm::TransactionTrait,
#transaction_trait_bounds
{
self.into_ex().delete(db)#await_
}
Expand All @@ -959,7 +975,7 @@ fn expand_active_model_ex<'a>(
#[doc = " Generated by sea-orm-macros"]
pub #async_ fn delete<'a, C>(self, db: &'a C) -> Result<sea_orm::DeleteResult, sea_orm::DbErr>
where
C: sea_orm::TransactionTrait,
#transaction_trait_bounds
{
let active_model: ActiveModelEx = self.into();
active_model.delete(db)#await_
Expand Down
40 changes: 40 additions & 0 deletions sea-orm-macros/tests/derive_active_model_ex_send_future_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#![deny(clippy::future_not_send)]

mod parent {
use sea_orm::entity::prelude::*;

#[sea_orm::model]
#[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "parent")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(has_many)]
pub children: HasMany<super::child::Entity>,
}

impl ActiveModelBehavior for ActiveModel {}
}

mod child {
use sea_orm::entity::prelude::*;

#[sea_orm::model]
#[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "child")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub parent_id: Option<i32>,
#[sea_orm(belongs_to, from = "parent_id", to = "id")]
pub parent: BelongsTo<Option<super::parent::Entity>>,
}

impl ActiveModelBehavior for ActiveModel {}
}

#[test]
fn generated_active_model_ex_mutation_futures_are_send() {
let _ = parent::ActiveModel::builder();
let _ = child::ActiveModel::builder();
}
Loading