mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
@@ -63,14 +63,14 @@ func (cc *CarCreate) Mutation() *CarMutation {
|
||||
|
||||
// Save creates the Car in the database.
|
||||
func (cc *CarCreate) Save(ctx context.Context) (*Car, error) {
|
||||
if err := cc.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
err error
|
||||
node *Car
|
||||
)
|
||||
if len(cc.hooks) == 0 {
|
||||
if err = cc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err = cc.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
@@ -78,6 +78,9 @@ func (cc *CarCreate) Save(ctx context.Context) (*Car, error) {
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = cc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cc.mutation = mutation
|
||||
node, err = cc.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
@@ -102,7 +105,8 @@ func (cc *CarCreate) SaveX(ctx context.Context) *Car {
|
||||
return v
|
||||
}
|
||||
|
||||
func (cc *CarCreate) preSave() error {
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (cc *CarCreate) check() error {
|
||||
if _, ok := cc.mutation.Model(); !ok {
|
||||
return &ValidationError{Name: "model", err: errors.New("ent: missing required field \"model\"")}
|
||||
}
|
||||
@@ -189,13 +193,13 @@ func (ccb *CarCreateBulk) Save(ctx context.Context) ([]*Car, error) {
|
||||
func(i int, root context.Context) {
|
||||
builder := ccb.builders[i]
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
if err := builder.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mutation, ok := m.(*CarMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err := builder.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
builder.mutation = mutation
|
||||
nodes[i], specs[i] = builder.createSpec()
|
||||
var err error
|
||||
|
||||
Reference in New Issue
Block a user