Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用Sequelize.js和PostgreSQL在关联模型上查询JSONB字段_Node.js_Postgresql_Typescript_Sequelize.js - Fatal编程技术网

Node.js 使用Sequelize.js和PostgreSQL在关联模型上查询JSONB字段

Node.js 使用Sequelize.js和PostgreSQL在关联模型上查询JSONB字段,node.js,postgresql,typescript,sequelize.js,Node.js,Postgresql,Typescript,Sequelize.js,我有两种型号的Foo和BarFoo有一个字段barId,因此有一个Bar对象与之关联。 我可以查询所有我的Foo对象,并将其关联的Bar对象也包括在内(我使用的是带有以下内容的TypeScript): 我可以查询条形图对象,并按内部字段1进行过滤,如下所示: Bar.findAll<Bar>({ where: { 'jsonb_field': { inner_field1: 'text to find' } } }); 到目前为止还不错。现在,让我们尝试查询Foo对象,包括Ba

我有两种型号的
Foo
Bar
Foo
有一个字段
barId
,因此有一个
Bar
对象与之关联。 我可以查询所有我的
Foo
对象,并将其关联的
Bar
对象也包括在内(我使用的是带有以下内容的TypeScript):

我可以查询
条形图
对象,并按
内部字段1
进行过滤,如下所示:

Bar.findAll<Bar>({
  where: { 'jsonb_field': { inner_field1: 'text to find' } }
});
到目前为止还不错。现在,让我们尝试查询
Foo
对象,包括
Bar
对象,并通过
internal\u field1
进行过滤:

Foo.findAll<Foo>({
  where: { '$bar.jsonb_field$': { inner_field1: 'text to find' } },
  include: [{ model: Bar }]
});
作为记录,我正确地包含了Bar对象,因为我可以按其他非JSONB属性进行筛选,例如:

Foo.findAll<Foo>({
  where: { '$bar.number_field$': 5 },
  include: [{ model: Bar }]
});
Foo.findAll({
其中:{'$bar.number_字段$':5},
包括:[{model:Bar}]
});
据我所知,问题在于Sequelize不知道
jsonb_字段的类型,因此在将对象传递到where查询时会抛出错误


是否有办法避免此错误,可以使用
sequelize.literal()
sequelize.json()

使用
sequelize.cast
$contains
运算符:

Foo.findAll<Foo>({
  where: { '$bar.jsonb_field$': {
    $contains: sequelize.cast('{ "inner_field1": "text to find" }', 'jsonb')
  },
  include: [{ model: Bar }]
});
这两种解决方案都容易受到攻击,以确保转义或删除所有可能导致问题的字符(
,…)


是的,我刚刚回答了我自己的问题。不客气。

如果你需要检查平等性,它会起作用。但是在[Op.Like]的情况下,例如,它没有帮助。
Foo.findAll<Foo>({
  where: { '$bar.jsonb_field$': { inner_field1: 'text to find' } },
  include: [{ model: Bar }]
});
Error: Invalid value [object Object]
    at Object.escape ({project_root}\node_modules\sequelize\lib\sql-string.js:50:11)
    at Object.escape ({project_root}\node_modules\sequelize\lib\dialects\abstract\query-generator.js:917:22)
    at Object.whereItemQuery ({project_root}\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2095:41)
    at _.forOwn ({project_root}\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1937:25)
    ...
Foo.findAll<Foo>({
  where: { '$bar.number_field$': 5 },
  include: [{ model: Bar }]
});
Foo.findAll<Foo>({
  where: { '$bar.jsonb_field$': {
    $contains: sequelize.cast('{ "inner_field1": "text to find" }', 'jsonb')
  },
  include: [{ model: Bar }]
});
Foo.findAll<Foo>({
  where: { '$bar.jsonb_field$': {
    $contains: sequelize.literal(`'{ "inner_field1": "text to find" }'::json`)
  },
  include: [{ model: Bar }]
});