【问题标题】:Type Orm where relation is a specific value or relation is null关系是特定值或关系为空的Typeorm
【发布时间】:2022-12-07 07:22:09
【问题描述】:

我想在一个查询中返回所有用户特定产品和一般产品(没有任何与用户映射的产品)..

我努力了

const query = this.productRepo
        .createQueryBuilder('products')
        .innerJoinAndSelect('products.users',
         'users',
        'users.id = 24 OR users.id IS NULL'
        )....more

但它不起作用 OR 工作正常,因为我已经尝试过 'users.id = 24 OR users.id = some other value 工作正常.. 我在这里做错了什么?

我的关系

@ManyToMany(() => User, {
    onUpdate: 'CASCADE',
    onDelete: 'CASCADE',
    nullable: true,
})
@JoinTable({
    name: 'product_user_mappings',
    joinColumn: {
        name: 'productId',
        referencedColumnName: 'id',
    },
    inverseJoinColumn: {
        name: 'userId',
    },
})
users: User[];

【问题讨论】:

    标签: node.js nestjs typeorm node.js-typeorm


    【解决方案1】:

    通常,我发现通过考虑 Venn Diagrams 来推断 SQL 连接是最简单的。我从你的问题中收集到的是,我们可能想要类似左内连接的东西。

    const query = this.productRepo
        .createQueryBuilder('products')
        .leftJoin('products.users', 'user')
        .where('user.id = :id', {id: 24})
        .orWhere('user.id IS NULL)
    

    【讨论】:

      猜你喜欢
      • 2021-06-10
      • 2020-06-30
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 2020-04-06
      • 2021-10-12
      • 2021-07-06
      • 2021-12-26
      相关资源
      最近更新 更多