小编alj*_*zen的帖子

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

我有我的两个模型FooBarFoo有一个 field barId,因此有一个Bar与之关联的对象。我可以查询所有Foo对象并包含它们的关联Bar对象(我将 TypeScript 与sequ​​elize-typescript一起使用):

Foo.findAll<Foo>({
  include: [{ model: Bar }]
});
Run Code Online (Sandbox Code Playgroud)

Barjsonb_field对象有一个带有结构的JSONB 字段

{ inner_field1: 'some text', inner_field2: 'some more text' }
Run Code Online (Sandbox Code Playgroud)

我可以查询Bar对象并按inner_field1如下方式进行过滤:

Bar.findAll<Bar>({
  where: { 'jsonb_field': { inner_field1: 'text to find' } }
});
Run Code Online (Sandbox Code Playgroud)

这会产生以下 SQL 查询:

SELECT ... FROM "Bar" AS "Bar" 
WHERE ("Bar"."jsonb_field"#>>'{inner_field1}') = 'text to find'
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都很好。现在让我们尝试查询Foo对象,包括Bar对象并按以下条件过滤inner_field1

Foo.findAll<Foo>({ …
Run Code Online (Sandbox Code Playgroud)

postgresql node.js sequelize.js typescript

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

node.js ×1

postgresql ×1

sequelize.js ×1

typescript ×1