You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error when using $contains operator with JSONB array field #5173
dialect: mysqlFor issues and PRs. Things that involve MySQL (and do not involve all dialects).dialect: postgresFor issues and PRs. Things that involve PostgreSQL (and do not involve all dialects).type: bugDEPRECATED: replace with the "bug" issue type
Hello, I have a table with a field doc which is of type JSONB. Each doc has a browsers array. I try to search for rows where doc->browsers contains some value using the $contains operator, like this:
ERROR: operator does not exist: text @> text[] at character 478
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Do you know how to support this use case in Sequelize? Thanks!
Reacted by Ricardo Seromenho, Daniel Hreben, Karl Adler, James Gentes, md, Luobo Zhang, Lukman Nuriakhmetov, Alex Mueller, Daniel Rodriguez del Villar Trimarchi, Matt Zeunert and 7 more
ERROR: operator does not exist: text @> text[] at character 39
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
STATEMENT:
SELECT "id", "doc", "createdAt", "updatedAt", "deletedAt"
FROM "Devices" AS "Device"
WHERE ("Device"."deletedAt" IS NULL AND ("Device"."doc"#>>'{browsers}') @> ARRAY['chrome'])
LIMIT 1;
Hmm yeah so the problem is likely that the syntax we use for looking up a nested element will return that item as a value, rather than as a json object.
It having to be an JSON array makes sense, and we should likely do that conversion.
Using the non string converting lookup when using $contains looks like the way to go.
Reacted by Martin Zagora, Justin, Derek Lu, Jaekwan Lee, gringParis, Luobo Zhang, Daniel Rodriguez del Villar Trimarchi, Indospace.io, Vincenzo Ferrari and mmikhalko
@vkm912 Not sure, I'm still using my workaround for certain use-cases with JSON Arrays in MySQL.
As @janmeier pointed out, in Postgres the JSONB field and the $contains operator is working,
not sure if it's the case now with MySQL JSON fields as well.
@vkm912 Not sure, I'm still using my workaround for certain use-cases with JSON Arrays in MySQL.
As @janmeier pointed out, in Postgres the JSONB field and the $contains operator is working,
not sure if it's the case now with MySQL JSON fields as well.
@mathieumagalhaes , Thanks for the reply. Yes I tried using $contains through Sequelize for MySQL, but that was getting converted into "#>" symbol which is not supported in MySQL. @janmeier : Please let us know in case if you got a chance to try your solution for MySQL as well.
@vkm912 There is no JSONB field in MySQL, only JSON DataType is supported in MySQL/Sequelize. Therefore his solution will not work with the MySQL dialect setup in Sequelize.
You could find a workaround for almost anything using 'literal' and 'fn' following the JSON MySQL documentation.
But it would be nice to see it being supported natively in Sequelize.
I'm just suggesting one way. If you consider performance, don't put JSONB ARRAY on where clause.
This issue is really the CAST that's needed to get sequelize to work, not your example. There is no issue with searching a jsonb array, especially once the appropriate GIN index has been created. I am able to search several million records on my pretty avg notebook and return results in 0.4ms which is acceptable for my use case.
Related: How can one run these 2 raw queries with sequelize.query and passing the array as a replacement, so it would handle escaping?
SELECT * FROM product where tags @> '["tag1", "tag2"]';
SELECT * FROM product where tags ?| ARRAY['tag1', 'tag2'];
product.tags is a JSONB array in Postgres.
I tried
let [recs, meta] = await sequelize.query(`SELECT * FROM product WHERE tags @> ?`, {
replacements: [['tag1', 'tag2']]
})
but sequelize produces SELECT * FROM product WHERE tags @> 'tag1', 'tag2'
If I add another set of [] (why would it be needed?), as in replacements: [[['tag1', 'tag2']]], then
then it produces SELECT * FROM product WHERE tags @> ('tag1', 'tag2')
The 2nd query also doesn't work, the ? in ?| operator is parsed as a replacement.
Thanks!
Could you open another issue for that?
It's a valid issue, and the related regexp needs to be improved, but let's keep this issue strictly about automatically casting to JSONB for this scenario
dialect: mysqlFor issues and PRs. Things that involve MySQL (and do not involve all dialects).dialect: postgresFor issues and PRs. Things that involve PostgreSQL (and do not involve all dialects).type: bugDEPRECATED: replace with the "bug" issue type
Activity
mickhansen commentedon Jan 9, 2016
Please post the complete SQL query when errors are related to SQL query generation.
buunguyen commentedon Jan 9, 2016
Here you go
mickhansen commentedon Jan 9, 2016
Hmm yeah so the problem is likely that the syntax we use for looking up a nested element will return that item as a value, rather than as a json object.
Can you show me the correct SQL for that query?
buunguyen commentedon Jan 10, 2016
I've found 3 queries that work:
#>to avoid casting, argument still needs to be JSON array?&operator so that argument can be ARRAY instead of JSONmickhansen commentedon Jan 10, 2016
It having to be an JSON array makes sense, and we should likely do that conversion.
Using the non string converting lookup when using
$containslooks like the way to go.buunguyen commentedon Jan 11, 2016
Sounds good. Hope there'll be a fix for this soon.
duylam commentedon Feb 28, 2016
Given json structure
I can get rows having
name = "chrome"inbrowsersarray using below queryHere is the generated SQL statement
marol-hadidjaja commentedon Mar 14, 2016
really need this bug fixed...
zaggino commentedon Mar 24, 2016
my current workaround:
aliatsis commentedon Apr 27, 2016
+1
keymsg commentedon Apr 27, 2016
+1
matthewheck commentedon Jun 7, 2016
I'm taking a look into fixing this. @mickhansen - Just to be clear, you recommend the first option from @buunguyen's list?
("Device"."doc"#>>'{browsers}')::jsonb @> '["chrome"]')mickhansen commentedon Jun 8, 2016
@matthewheck I think i simply agreed that we need to convert the value to a JSON array rather than a PG array.
22 remaining items
mathieumagalhaes commentedon Oct 16, 2018
@vkm912 Not sure, I'm still using my workaround for certain use-cases with JSON Arrays in MySQL.
As @janmeier pointed out, in Postgres the JSONB field and the $contains operator is working,
not sure if it's the case now with MySQL JSON fields as well.
vkm912 commentedon Oct 17, 2018
@mathieumagalhaes , Thanks for the reply. Yes I tried using $contains through Sequelize for MySQL, but that was getting converted into "#>" symbol which is not supported in MySQL.
@janmeier : Please let us know in case if you got a chance to try your solution for MySQL as well.
mathieumagalhaes commentedon Oct 17, 2018
@vkm912 There is no JSONB field in MySQL, only JSON DataType is supported in MySQL/Sequelize. Therefore his solution will not work with the MySQL dialect setup in Sequelize.
You could find a workaround for almost anything using 'literal' and 'fn' following the JSON MySQL documentation.
But it would be nice to see it being supported natively in Sequelize.
haracejacob commentedon Mar 20, 2019
In PostgreSQL, Can be used
$containsoperator with JSONB ARRAY by..Data Format
{ "doc": { "browsers": ["foo", "bar"] } }In Sequelize
query
update 2022.04.07
if you want to use an index, you shouldn't use the code above.
In my case, I just use literal.
Data Format
{ "doc": { "browsers": ["foo", "bar"] } }In Sequelize
samuelgoldenbaum commentedon Nov 12, 2019
The cast here has big performance hit, especially with large datasets
haracejacob commentedon Nov 12, 2019
@samuelgoldenbaum
I'm just suggesting one way. If you consider performance, don't put JSONB ARRAY on where clause.
samuelgoldenbaum commentedon Nov 12, 2019
This issue is really the CAST that's needed to get sequelize to work, not your example. There is no issue with searching a jsonb array, especially once the appropriate GIN index has been created. I am able to search several million records on my pretty avg notebook and return results in 0.4ms which is acceptable for my use case.
See issue #11671 I have created.
PS, I am currently resorting to raw queries for this scenario, similar to:
haracejacob commentedon Nov 12, 2019
@samuelgoldenbaum Yes, you're right.
I also created a similar issue #10599. but closed.
afzalIbnSH commentedon Feb 3, 2022
What worked for me;
chenzewang commentedon Apr 4, 2022
that work for me ,thx bro
mariusa commentedon Apr 10, 2022
Related: How can one run these 2 raw queries with
sequelize.queryand passing the array as a replacement, so it would handle escaping?product.tagsis a JSONB array in Postgres.I tried
but sequelize produces
SELECT * FROM product WHERE tags @> 'tag1', 'tag2'If I add another set of
[](why would it be needed?), as inreplacements: [[['tag1', 'tag2']]], thenthen it produces
SELECT * FROM product WHERE tags @> ('tag1', 'tag2')The 2nd query also doesn't work, the
?in?|operator is parsed as a replacement.Thanks!
ephys commentedon Apr 10, 2022
Could you open another issue for that?
It's a valid issue, and the related regexp needs to be improved, but let's keep this issue strictly about automatically casting to JSONB for this scenario
Edit: the other issue is #14410