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

database/gdb: Using gdb.Raw conditions in where when querying does not meet expectations #3649

Open
hailaz opened this issue Jun 19, 2024 · 0 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@hailaz
Copy link
Member

hailaz commented Jun 19, 2024

Go version

go version go1.20.12 windows/amd64

GoFrame version

2.7.0

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

func TestSelectRaw(t *testing.T) {
	table := createInitTable()
	defer dropTable(table)
	gtest.C(t, func(t *gtest.T) {
		user := db.Model(table)
		n, err := user.Where(g.Map{
			"id = ?":          gdb.Raw("id+100"),
			"create_time = ?": gdb.Raw("now()"),
		}).Count()
		t.AssertNil(err)
		t.Assert(n, 1)
	})
}

What did you see happen?

SELECT COUNT(1) FROM `user_1718767159000601600` WHERE id = ?id+100 AND create_time = ?now()

报错
Error: Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?id+100 AND create_time = ?now()' at line 1

What did you expect to see?

期望是能使用函数,如下面的方式就正常。

SELECT COUNT(1) FROM `user_1718766980463715400` WHERE (create_time = now()) AND (`create_time` < now())
func TestSelectRaw(t *testing.T) {
	table := createInitTable()
	defer dropTable(table)
	gtest.C(t, func(t *gtest.T) {
		user := db.Model(table)
		n, err := user.Where("create_time = ?", gdb.Raw("now()")).WhereLT("create_time", gdb.Raw("now()")).Count()
		t.AssertNil(err)
		t.Assert(n, 1)
	})
}

Where("create_time = ?", gdb.Raw("now()"))这样就正常,Where("create_time", gdb.Raw("now()"))这样也不行

@hailaz hailaz added the bug It is confirmed a bug, but don't worry, we'll handle it. label Jun 19, 2024
@Issues-translate-bot Issues-translate-bot changed the title database/gdb: 查询时在where中使用gdb.Raw条件不符合预期 database/gdb: Using gdb.Raw conditions in where when querying does not meet expectations Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

1 participant