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

json query with boolean argument fails #7005

Open
andersryanc opened this issue Apr 30, 2024 · 3 comments
Open

json query with boolean argument fails #7005

andersryanc opened this issue Apr 30, 2024 · 3 comments
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@andersryanc
Copy link

andersryanc commented Apr 30, 2024

GORM Playground Link

go-gorm/playground#729

Description

When querying a property inside a json field with a bool argument, the query returns 0 rows. However, if you embed the bool into the where clause itself, you get 1 row. The strange thing is that the generated SQL shown by the debugger is identical.

Broken Example:

DB.Where("config->'enabled' = ?", true).Find(&results)
2024/04/30 11:20:10 /__REDACTED__/playground/main_test.go:108
[0.847ms] [rows:0] SELECT * FROM `accounts` WHERE config->'$.enabled' = true

Working Example:

DB.Where("config->'enabled' = true").Find(&results)
2024/04/30 11:20:10 /__REDACTED__/playground/main_test.go:108
[0.647ms] [rows:1] SELECT * FROM `accounts` WHERE config->'$.enabled' = true

I tried testing a variety of other variations as well, like using json_extract and the double arrow syntax, like config->>'$.enabled'. The same thing also happens with nested values, such as config->'$.foo.enabled' = true.

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Apr 30, 2024
@github-actions github-actions bot added type:with reproduction steps with reproduction steps and removed type:missing reproduction steps missing reproduction steps labels Apr 30, 2024
@seahm
Copy link

seahm commented May 24, 2024

@andersryanc
I think in sql driver true/false=>1/0. DB.Where("config->'enabled'` = ?", true).Find(&results) would work if your json field config->'enabled' is set to 1/0 instead of true/false. For example:
Screenshot 2024-05-24 at 10 01 55 AM

@andersryanc
Copy link
Author

I could certainly try that... but...

The thing that doesn't make sense to me is that the SQL expression itself works fine using a boolean. As you can see in my description above, the SQL that is output is identical in both cases. You can also run the SQL directly through a SQL prompt or GUI application and get the expected results. What doesn't make sense is why the GORM Where() function does not return the results when passing the boolean value as a parameter in the 2nd argument, versus when you hard code the boolean into the SQL expression itself in the 1st argument of the function (and pass no parameter in the 2nd argument).

You can see that in both cases, the SQL output is the same, while one says [rows:0] and the other says [rows:1].

@seahm
Copy link

seahm commented May 28, 2024

I am not very familiar with this. But I think DB.Where("config->'enabled' = true").Find(&results) and DB.Where("config->'enabled' = ?", true).Find(&results) do not take the exact route. You will see the difference if you step into the code. The SQL output is just the logging. If you set ParameterizedQueries of your logger config to true, it will display differently.

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

3 participants