I tried to select a record with the code below.
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("description = ?", item.Description).First(&exist).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
err = r.db.Create(&entity.Item{
Name: item.Name,
Description: item.Description,
Quantity: item.Quantity,
OrderID: order.ID,
}).Error
if err != nil {
return err
}
}
But, the program stops and doesn’t execute the create function. How can I handle the error?
I expect the error to be caught by err
and then the program executes inserting new data to database.
The log:
record not found
[5.093ms] [rows:0] SELECT * FROM "items" WHERE description = 'iPhone' ORDER BY "items"."id" LIMIT 1 FOR UPDATE
New contributor
rsktrk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.