Closed
Description
GORM Playground Link
Description
First function:
Code:
var tDB appmodels.TaskDB
if err = getgDB().WithContext(ctx).Table("task_db").Where("task_id = ?", taskID).First(&tDB).Error; err != nil {
return nil, err
}
// The following is printed by gorm logger - level INFO
// SELECT * FROM "task_db" WHERE task_id = '37-5' AND "task_db"."deleted_at" IS NULL ORDER BY "task_db"."task_id" LIMIT 1
This function return an err != nil if record not exist.
Find function:
Code:
var tDB appmodels.TaskDB
if err = getgDB().WithContext(ctx).Table("task_db").Where("task_id = ?", taskID).Find(&tDB).Error; err != nil {
return nil, err
}
// The following is printed by gorm logger - level INFO
// SELECT * FROM "task_db" WHERE task_id = '37-5' AND "task_db"."deleted_at" IS NULL
This function return an err == nil if record not exist.
Conclusion:
While the first function can be used but it will cost some overhead for ordering the result.
I think the reason is Find function update the DeletedAt fields while First funtion doesn't.