This is the JSONB array data type in the model and postgres
database:
.
.
.
MyField: {
type: DataTypes.ARRAY(DataTypes.JSONB),
allowNull: false
}
.
.
.
The table field contains JSONB values:
{"{\"LessonId\": \"1\", \"TeacherId\": \"1\"}"}
And this is my where clause in sequelize findAll method:
where: {
MyField: {
[Op.contains]: [
{
TeacherId: '1',
}
]
}
}
The generated query is this:
SELECT
...
"MyField",
FROM
"MyTable" AS "MyTable"
WHERE
"MyTable"."MyField" @> ARRAY [ '{"TeacherId":"1"}' ]:: JSONB [];
And the result set is empty. But when i include LessonId
too, it will do my answer.
I found postgres sees the jsonb object like a String text. What is the correct way to query based on specific key in JSONB ARRAY?