go-zero
go-zero copied to clipboard
ID, UpdateAt,CreateAt not automatically generated when inserting data with mongo
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
-
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 }
-
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.
Environments (please complete the following information):
- OS: mac
- go-zero version v1.3.5
- goctl version 1.3.9 darwin/arm64
改为
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
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
This issue is stale because it has been open for 30 days with no activity.