Repository navigation
Description
Issue Creation Checklist
- I understand that my issue will be automatically closed if I don't fill in the requested information
- I have read the contribution guidelines
Bug Description
when I find data through json type field use Op on json data's boolean attribute or no attribute (use null filter), others are ok but mysql is not.
it seems that the problem is caused by mysql query-generator.js _toJSONValue method
when query mysql true or null is converted to string 'true' or 'null' , Generally, there is no problem but in mysql json datatype, null is null,not 'null' . and also true is true not 'true',So it caused this problem
Reproducible Example
Here is the link to the SSCCE for this issue:
https://github.com/sequelize/sequelize-sscce/actions/runs/3400701706/jobs/5655301066
What do you expect to happen?
SELECT `id`, `name`, `schoolInfo` FROM `Foos` AS `Foo` WHERE json_unquote(json_extract(`Foo`.`schoolInfo`,'$.\"isRegister\"')) IS null; What is actually happening?
SELECT `id`, `name`, `schoolInfo` FROM `Foos` AS `Foo` WHERE json_unquote(json_extract(`Foo`.`schoolInfo`,'$.\"isRegister\"')) IS 'null';{ Error: 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 ''null'' at line 1
Environment
- Sequelize version:
6.x - Node.js version:
16.x - If TypeScript related: TypeScript version:
- Database & Version: mysql 5.7
- Connector library & Version:
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I will need guidance.
- No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
- No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

Activity
WikiRik commentedon Nov 5, 2022
For the person that will look into this:
We have a test that is related to this behaviour, but it doesn't use operators;
sequelize/test/integration/model/json.test.js
Lines 386 to 421 in 76cc97f
ephys commentedon Nov 5, 2022
This is "normal" and not caused by
_toJSONValue.In MySQL, the JSON column and JSON functions just return text. It's not a true null nor boolean value. This can be demonstrated by running the following SQL:
You can't use
Op.isin nested JSON in MySQLIf we need to fix something, it's replacing
Op.iswith=in MySQL when using JSON (but I don't think we should. This should be documented instead)lyf-coder commentedon Nov 6, 2022
Thanks for your reply,My expression is not very accurate. I mean null because this attribute is not stored, such as in your example,I insert data:
It's not have employment attribute, I want to find out the records that do not contain this attribute,Is there any way to do it ? @WikiRik
lyf-coder commentedon Nov 6, 2022
@ephys Thanks for your reply,
I want to find second and third records through attribute 'b' , Is there any way to do it ?
ephys commentedon Nov 6, 2022
Use
Op.eqinstead ofOp.islyf-coder commentedon Nov 6, 2022
@ephys thanks again!
but when use
Op.eq,sql is :
SELECT * FROM test WHERE json_unquote(json_extract(json,'$.\"b\"')) = 'null';it can't find third record:{"a": null}I need sql is :
SELECT * FROM test WHERE json_unquote(json_extract(json,'$.\"b\"')) is null;At present, it seems that no operator can do it. 😂ephys commentedon Nov 6, 2022
Ah yes sorry I misread that. Do this:
It will bypass the JSON serialization and produce
instead of
If you want to catch both MySQL's null and JSON's null:
Which will produce this:
MySQL's support for JSON is a bit poor imo 😬
ephys commentedon Nov 6, 2022
I'm labelling this as
docs, we'll need to document this caveatlyf-coder commentedon Nov 7, 2022
@ephys Thank you very much. It was solved,👍👍👍
ephys commentedon Nov 7, 2022
I'll keep this open a little bit longer to remind us to document this :)