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

or condition in one condition sql error #4914

Closed
qianweirong opened this issue Dec 10, 2021 · 2 comments · Fixed by #4969
Closed

or condition in one condition sql error #4914

qianweirong opened this issue Dec 10, 2021 · 2 comments · Fixed by #4969
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@qianweirong
Copy link

GORM Playground Link

go-gorm/playground#411

Description

or condition在只有一个条件的情况下,生成SQL存在问题。

func TestGORMorCondition(t *testing.T) {
	user := User{Name: "jinzhu"}
	DB.Create(&user)

	ret := make([]*User, 0)
	id := []uint32{1}
	db := DB
	for _, id := range id {
		db = db.Or("id = ?", id)
	}
	db.Find(&ret)
}

生成的执行SQL如下:
image

这段代码在判断or条件时,是否存在问题
image

@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Dec 10, 2021
@ghost
Copy link

ghost commented Dec 28, 2021

https://gorm.cn/zh_CN/docs/advanced_query.html#Group-%E6%9D%A1%E4%BB%B6
其实这也是符合预期~,可以看下上面的文档 😘

@ghost ghost closed this as completed Dec 29, 2021
@qianweirong
Copy link
Author

这不符合预期吧。正常的语句应该是deleted_at IS NULL and id = 1。deleted_at是软删除标志位,如果条件变成deleted_at IS NULL or id = 1,那不是表中任意一条没有删除的数据都可以满足需求了。而且,在v1版本中,生成的SQL就是deleted_at IS NULL and id = 1。

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

Successfully merging a pull request may close this issue.

2 participants