简体   繁体   English

如何使用 sequelize 对包含表的 jsonb 列应用 where 子句?

[英]how to apply where clause for included table's jsonb column using sequelize?

I have 2 tables in postgres database.我在 postgres 数据库中有 2 个表。

Table_A
--------
id BIGINT
metadata JSONB

Table_B
--------
id BIGINT
a_id BIGINT
metadata JSONB

I am using sequelize and trying to apply where cluse for included table's jsonb column.我正在使用 sequelize 并尝试将 where cluse 应用于包含表的 jsonb 列。

Example:例子:

let query = {
        where: {
            '$table_b.metadata.address$': 'Banglore'
        }
        include: [
            {
                model: Table_B,
                as: 'table_b'
            }
        ]
    };
    
Table_A.findAndCountAll(query).then((result)=>{
    console.log(JSON.stringify(result.rows));
}).catch((err)=>{
    console.log(err)
})

But I am getting below error但我得到以下错误

original: error: missing FROM-clause entry for table "table_b->metadata"
  at Parser.parseErrorMessage (C:\Project\test\FilterTest\node_modules\pg-protocol\dist\parser.js:287:98)
  at Parser.handlePacket (C:\Project\test\FilterTest\node_modules\pg-protocol\dist\parser.js:126:29)
  at Parser.parse (C:\Project\test\FilterTest\node_modules\pg-protocol\dist\parser.js:39:38)
  at Socket.<anonymous> (C:\Project\test\FilterTest\node_modules\pg-protocol\dist\index.js:11:42)
  at Socket.emit (events.js:400:28)
  at addChunk (internal/streams/readable.js:293:12)
  at readableAddChunk (internal/streams/readable.js:267:9)
  at Socket.Readable.push (internal/streams/readable.js:206:10)
  at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {

Can anyone help how to apply where clause for included table's jsonb column?谁能帮助如何为包含表的 jsonb 列应用 where 子句?

Try this way.试试这个方法。

const query = {
    include: [{
        model: Table_B,
        as: 'table_b',
        where: {
            metadata: {
                address: 'Banglore'
            }
        }
    }]
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM