TypeORM选择实体,除了一些,其中id不等于条件

Вла*_*лов 17 typescript typeorm

我有两个实体:

    @Entity()
    export class Point {
    
         @PrimaryGeneratedColumn('uuid')
         id: string;
    
         // some other stuff
 
    }


    @Entity()
    export class Product {
    
         @PrimaryGeneratedColumn('uuid')
         id: string;
    
         @IsOptional()
         @ManyToMany(() => Point)
         @JoinTable()
         prohibitedToSaleOn: Point[];
    
    }
Run Code Online (Sandbox Code Playgroud)

我想要获得产品,其中prohibitedToSaleOn(数组Point)中的任何对象都满足条件

point.id != {idWhatIWant}

所以,最后我想获得所有产品,而不是在选定的点禁止销售。我做了这样的事情:

    return this.productRepository.createQueryBuilder('product')
        .leftJoin('product.prohibitedToSaleOn', 'point')
        .where('point.id != :id', {id})
        .getMany();
Run Code Online (Sandbox Code Playgroud)

但它不起作用(根本不应该)

我需要有关正确请求的帮助。谢谢=)

PS我使用PostgreSQL

Vla*_*tko 29

我不确定您是否需要使用查询生成器。在这里您可以找到使用关系编写联接的替代方法的描述。

要通过 id 不等于您提供的 id ( .where('point.id != :id', {id})) 来过滤它们,您可以编写类似 的内容find({where: {id: Not(id)}}),其中Not是从 TypeORM 导入的。


Fil*_*vic 5

我发现最简单的方法是使用typeorm's NotOperator,如下所示:

return this.productRepository.find( { where: id: Not('some_id') } );
Run Code Online (Sandbox Code Playgroud)

有关以下内容的文档Not
查找选项运算符。用于否定表达式。示例: { title: not("hello") } 将返回 title 不等于“hello”的实体。

在这里阅读更多内容