8000 Find doesn't return error if record not found · Issue #3320 · go-gorm/gorm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Find doesn't return error if record not found #3320
Closed
@thanhpp

Description

@thanhpp

GORM Playground Link

go-gorm/playground#93

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0