Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize: Querying if ARRAY contains a value

Suppose I have a PG ARRAY field:

id |    array    |
===|=============|
  1|{"1","2","3"}|

How do I use sequelize to query to see if the array field as the value 1.

I tried:

array: { $contains: "1" }

which gives me:

array @> "1"

with error:

Possibly unhandled SequelizeDatabaseError: array value must start with "{" or dimension information

UPDATE

I was able to do it by: array: { $contains: '{' + value + '}' }

Is there a more correct way?

like image 416
Calvintwr Avatar asked Mar 13 '15 15:03

Calvintwr


1 Answers

I realised that sequelize is expecting the condition to be an array:

array: { [Op.contains]: ["1"] }

That will work. Cheers!!

Bear in mind that Op is exported from sequelize

const { Op } = require('sequelize');

or

import { Op } from 'sequelize';

See official docs: https://sequelize.org/master/manual/model-querying-basics.html#operators

like image 79
Calvintwr Avatar answered Sep 17 '22 13:09

Calvintwr