postgresql - Postgres中的Typeorm查询数组类型
问题描述
const query = userConnection.getRepository(User).createQueryBuilder('user');
const result = await query.where("user.role @> ARRAY[:role]", { role: 'Super-Admin' })
.getMany();
return result;
实体
@Entity()
export class User extends BaseEntity{
@Column({type: 'text', array: true})
role: UserRole[];
}
我收到错误'操作员不存在:文本@>文本[]'
我在这里做错了什么?
解决方案
https://www.postgresql.org/docs/9.1/functions-comparisons.html
expression operator ANY (array expression)
右边是一个带括号的表达式,它必须产生一个数组值。使用给定的运算符计算左侧表达式并与数组的每个元素进行比较,该运算符必须产生布尔结果。如果获得任何真结果,则 ANY 的结果为“真”。如果没有找到真正的结果(包括数组有零元素的情况),则结果为“假”。
所以你可以使用ANY
函数来查找用户具有多个包含管理员的角色
const result = await query.where(':role = ANY (user.role)', { role: 'Super-Admin' });
推荐阅读
- javascript - Kendo DatePicker 使用 javascript 清除输入,占位符被删除
- angular - 增加一个div的高度以降低另一个div的高度?
- php - 如何在 Ubuntu 16.04 机器上安装带有 Apache 2.0 的 PHP 5.x 版本和带有 Apache 2.4 的 PHP 7.x 版本
- sql-server - sql server 中的多列数据透视
- c# - c#事件触发后按正确顺序调用方法
- ios - 如果 segue 已在使用中,则从 previos VC 传回数据
- javascript - TypeScript:从函数调用中解构对象的“任何”
- sql - 从表中删除日期语句错误
- c++ - 是否可以在片段着色器中写入特定的 mipmap 级别?
- java - Aspect 仅适用于 Aspect 类所在包上的类