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

使用GetFrom....之类的函数进行查找时,怎么判断是否存在 #196

Open
jiang4869 opened this issue Jan 13, 2022 · 2 comments

Comments

@jiang4869
Copy link
Collaborator

例如调用这个方法进行查询的时候,如果判断找的数据是否存在。使用Find查找的时候,如果没有数据存在,会填入默认值,并且err也为nil。 是否应该把结果的RowsAffected 也一起返回,通过这个来判断是否找到数据

func (obj *_AdminMgr) GetFromID(id uint) (result Admin, err error) {
	err = obj.DB.WithContext(obj.ctx).Model(Admin{}).Where("`id` = ?", id).Find(&result).Error

	return
}
@xxjwxc
Copy link
Owner

xxjwxc commented Jan 13, 2022

通过id可以判断,

@redbirdztc
Copy link

... .Model(Admin{}) ...

这里是不是可以直接使用result而不是再去新建一个结构体

func (obj *_AdminMgr) GetFromID(id uint) (result Admin, err error) {
	err = obj.DB.WithContext(obj.ctx).Model(result).Where("`id` = ?", id).Find(&result).Error
	return
}

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

No branches or pull requests

3 participants