go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

ID, UpdateAt,CreateAt not automatically generated when inserting data with mongo

Open MiHuaaaaaaaa opened this issue 1 year ago • 2 comments

Describe the bug A clear and concise description of what the bug is. ID, UpdateAt,CreateAt not automatically generated when inserting data with mongo

To Reproduce Steps to reproduce the behavior, if applicable: Using the goctl model mongo generated Insert function

  1. The code is

    
         user = &model.User{Phone: req.Phone, Password: password}
    
     if err = l.svcCtx.UserModel.Insert(l.ctx, user); err != nil {
     	return nil, err
     }
    
    
         func (m *defaultUserModel) Insert(ctx context.Context, data *User) error {
           if !data.ID.IsZero() {
     	      data.ID = primitive.NewObjectID()
     	      data.CreateAt = time.Now()
     	      data.UpdateAt = time.Now()
           }
    
           _, err := m.conn.InsertOne(ctx, data)
           return err
       }
    
  2. The error is

    info 	mongo(users) - InsertOne - ok - [{"id":"000000000000000000000000","updateAt":"0001-01-01T00:00:00Z","createAt":"0001-01-01T00:00:00Z","phone":"xxxxxx","password":"333130"}]
    

Expected behavior A clear and concise description of what you expected to happen. ID, UpdateAt,CreateAt can be generated automatically Screenshots If applicable, add screenshots to help explain your problem. image

Environments (please complete the following information):

  • OS: mac
  • go-zero version v1.3.5
  • goctl version 1.3.9 darwin/arm64

MiHuaaaaaaaa avatar Jul 12 '22 09:07 MiHuaaaaaaaa

改为

func (m *defaultUserModel) Insert(ctx context.Context, data *User) error {
	if data.ID.IsZero() {
		data.ID = primitive.NewObjectID()
		data.CreateAt = time.Now()
		data.UpdateAt = time.Now()
	}

	_, err := m.conn.InsertOne(ctx, data)
	return err
}

应该就正确了 @MiHuaaaaaaaa

czyt avatar Jul 13 '22 01:07 czyt

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


change to

func (m *defaultUserModel) Insert(ctx context.Context, data *User) error {
if data.ID.IsZero() {
data.ID = primitive.NewObjectID()
data.CreateAt = time.Now()
data.UpdateAt = time.Now()
}

_, err := m.conn.InsertOne(ctx, data)
return err
}

That should be correct @MiHuaaaaaaaa

Issues-translate-bot avatar Jul 13 '22 01:07 Issues-translate-bot

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Aug 12 '22 02:08 github-actions[bot]