Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find doesn't return error if record not found #3320

Closed
thanhpp opened this issue Aug 28, 2020 · 2 comments
Closed

Find doesn't return error if record not found #3320

thanhpp opened this issue Aug 28, 2020 · 2 comments
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@thanhpp
Copy link

thanhpp commented Aug 28, 2020

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.

@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Aug 28, 2020
@jinzhu
Copy link
Member

jinzhu commented Aug 28, 2020

this is the expected behavior, http://v2.gorm.io/docs/query.html#Retrieving-a-single-object

@jinzhu jinzhu closed this as completed Aug 28, 2020
@thanhpp
Copy link
Author

thanhpp commented Aug 28, 2020

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

2 participants