简体   繁体   中英

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

I have 2 tables in postgres database.

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.

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?

Try this way.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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