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

[5.8] Querying on json columns with boolean values broken #27696

Closed
mvanduijker opened this issue Feb 27, 2019 · 14 comments · Fixed by #27847
Closed

[5.8] Querying on json columns with boolean values broken #27696

mvanduijker opened this issue Feb 27, 2019 · 14 comments · Fixed by #27847
Labels

Comments

@mvanduijker
Copy link

  • Laravel Version: 5.8.0
  • PHP Version: 7.3.2
  • Database Driver & Version: Mysql 8

Description:

Trying to query on a database column of type json with a boolean value doesn't return results.

Example:

$query->where('myColumn->boolean', true);

There is a workaround if we make the boolean value a string, the query works again.

$query->where('myColumn->boolean', 'true');

Not really intuitive right now. Probably broken by #25732

Steps To Reproduce:

create table example
(
	field json null
);

INSERT INTO example (field) VALUES ('{"yes": true}');
DB::table('example')->where('field->yes', true)->get();
@driesvints
Copy link
Member

Ping @staudenmeir

@staudenmeir
Copy link
Contributor

Already on it.

@staudenmeir
Copy link
Contributor

staudenmeir commented Feb 27, 2019

Boolean comparisons don't work with PostgreSQL or SQL Server either, but – as opposed to MySQL – they never have.

Unfortunately, where('field->yes', 'true') is not an ideal workaround, as it also matches "true" strings.

This is a complex issue, I'm not sure whether we can fix it.

We would have to remove the json_unquote() call for boolean values, but when the grammar compiles the SQL statement, it doesn't know what value the statement is for.

The json_unquote() call is actually only necessary for the SELECT clause, but I don't see a realistic way to compile different statements for the SELECT and the WHERE clause.

@ctxcode
Copy link

ctxcode commented Feb 28, 2019

Another work around is:

$query->whereRaw('JSON_EXTRACT(myColumn, "$.boolean") = true');

@setkyar
Copy link

setkyar commented Feb 28, 2019

@LorenzV No, that does not work. It's not that simple.

@aaronhuisinga
Copy link

@staudenmeir were you able to make any more progress on this? We're stuck waiting to see if there is anything that can be done about this before finalizing our upgrade to 5.8.

@staudenmeir
Copy link
Contributor

@aaronhuisinga Not really. Using whereRaw() is the best workaround at the moment.

@driesvints
Copy link
Member

@setkyar why doesn't that work for your use case?

@driesvints
Copy link
Member

@staudenmeir there's no way we might implement something custom for boolean values?

@cmorbitzer
Copy link
Contributor

We ought to make it work. This is a silent breaking change, resulting in a potentially dangerous data situation.

Of course, it is possible to fix it. It's just that the solution may not look neat.

Perhaps instead of using json_unquote we could go back to using ->> under the hood, but make it seamless for the user by doing a transformation from -> to ->> behind the scenes depending on the driver? Not sure if that would work.

I don't think it will come to this, but rolling back #25732 would still be better since that was an annoyance but not truly a bug. If we supported non-string data types in the last Laravel version, we should continue supporting it in order to claim JSON support.

@driesvints
Copy link
Member

@cmorbitzer we're currently not considering rolling back that PR. We should focus on fixing the current issue instead.

@staudenmeir
Copy link
Contributor

I'm working on a solution, it will be ready tomorrow.

@driesvints
Copy link
Member

@staudenmeir thanks!

@staudenmeir
Copy link
Contributor

#27847

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

Successfully merging a pull request may close this issue.

7 participants