Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@booleangate
Copy link
Owner

@booleangate booleangate commented May 5, 2022

Fixable in https://github.com/ent/ent with

diff --git a/entc/gen/template/builder/create.tmpl b/entc/gen/template/builder/create.tmpl
index 62f9ecdb..b9afe79d 100644
--- a/entc/gen/template/builder/create.tmpl
+++ b/entc/gen/template/builder/create.tmpl
@@ -83,8 +83,10 @@ func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) (*{{ $.Name }},
                        }
                        mut = {{ $receiver }}.hooks[i](mut)
                }
-               if _, err := mut.Mutate(ctx, {{ $mutation }}); err != nil {
+               if v, err := mut.Mutate(ctx, {{ $mutation }}); err != nil {
                        return nil, err
+               } else if v, ok := v.(*{{ $.Name }}); ok {
+                       node = v
                }
        }

@booleangate booleangate changed the title Downstream opcreate model propagation Downstream OpCreate model propagation May 5, 2022
// client.User.Create()....Save(ctx) receives this updated value. The bug is that since we're doing a
// downstream mutation (e.g. mutating after the call to next.Mutate instead of before), this updated
// User value is not propagated to the call-site.
return newUser, nil
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 this is the bit that expresses this bug. Any value returned after calling next.Mutate is ignored.

}

func hashID(id int, salt string) (string, error) {
hd := hashids.NewData()
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📓 I've added this dependency to make this bug example follow a real world use case instead of something contrived.

@booleangate
Copy link
Owner Author

Fixed in ent/ent#2525

@booleangate booleangate closed this May 5, 2022
@booleangate booleangate deleted the downstream-opcreate-model-propagation branch May 5, 2022 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants