"QueryFailedError: operator does not exist",如何使用PostgreSQL数组包含带typeorm的运算符?



我将TypeOrm与TypeScript一起使用,我有一个类似的products表。

export enum ColorTone {
Cambridge = 'GREY_TONE',
Nicolock = 'RED_TONE',
Keystone = 'BLUE_TONE',
TechoBloc = 'YELLOW_TONE',
}
@Entity('product')
export class Product {
@Column()
name: string;
@Column({
type: 'enum',
enum: Brand,
array: true,
default: [],
})
colorTones: ColorTone[]; // available colors for a product
}

我想找到所有含有特定色调的产品。我使用TypeOrm查询进行了尝试。

...
constructor(@InjectRepository(Product) private productsRepository: Repository<Product>) {}
...
findProducts(colorTone: ColorTone): Promise<Product[]> {
return this.productsRepository.createQueryBuilder('product')
.where('product.colorTones @> ARRAY[:colorTone]', { colorTone });
}

但我得到这个控制台错误。

QueryFailedError: operator does not exist: product_colortones_enum[] @> text[]
...

不确定是否有办法进行类型转换。

我整天都在努力寻找解决方案,任何相关链接或参考链接都将不胜感激。

您需要转换为正确的类型。

.where('product.colorTones @> ARRAY[:colorTone::product_colortones_enum]', { colorTone });

如果typeorm被冒号沙拉弄糊涂了,请将强制转换移到数组

.where('product.colorTones @> ARRAY[:colorTone]::product_colortones_enum[]', { colorTone });

最新更新