【问题标题】:TypeOrm, Postgres & NestJS: filter ManyToMany columnTypeOrm、Postgres 和 NestJS:过滤 ManyToMany 列
【发布时间】:2021-07-21 01:37:18
【问题描述】:

我在开发 Web 应用程序时使用了 postgresSQL、Typeorm 和 NestJs。 我有几个实体:

@Entity()
export class Product {
  @PrimaryGeneratedColumn()
  id: string;

  @Column()
  name: string;

  @ManyToMany(
    () => Property,
    { nullable: true, eager: true },
  )
  @JoinTable()
  properties: Property[];
}

@Entity()
export class Property {
    @PrimaryGeneratedColumn()
    id: string;

    @Column({ unique: true })
    key: string;

    @Column()
    name: string
}

我正在尝试按属性获取产品。 这在 sql 中给出:

;with r as (
    SELECT
           bien.*,
           ARRAY['usage.toto'::varchar] = array_agg(p.key) as compare1
    from bien
    INNER JOIN bien_properties_property bpp on bien.id = bpp."bienId"
    INNER JOIN property p on p.id = bpp."propertyId"
    GROUP BY bien.id
)
select * from r
where compare1

有人可以帮我吗?

谢谢

【问题讨论】:

    标签: typescript postgresql nestjs typeorm


    【解决方案1】:

    要访问Property 表上的Product 表,您需要声明双向关系,如here 所述。因此,它将是:

    @Entity()
    export class Product {
      @PrimaryGeneratedColumn()
      id: string;
    
      @Column()
      name: string;
    
      @ManyToMany(
        () => Property,
        property => property.products,
        { nullable: true, eager: true },
      )
      @JoinTable()
      properties: Property[];
    }
    
    @Entity()
    export class Property {
        @PrimaryGeneratedColumn()
        id: string;
    
        @Column({ unique: true })
        key: string;
    
        @Column()
        name: string
    
        @@ManyToMany(
          () => Product,
          product => product.properties,
        )
        products: Product[];
    }
    
    

    现在,要获得他们产品的属性,您需要像这样加入:

    //...
    
    const propertyRepository = connection.getRepository(Property);
    const properties = await propertyRepository.find({ relations: ["products"] });
    
    //...
    

    【讨论】:

    • 它不起作用,我试过了: let queryBuilder = this.productRepository .createQueryBuilder("product") .leftJoin('product.properties', 'property') .where("('usage .stockage') IN properties") return queryBuilder.getManyAndCount(),我得到一个错误
    • 我认为你的问题是在whereusage 是什么?您曾说过要“按属性获取产品”。在上面显示的代码中,您尝试按产品获取属性。我建议你试试这个:this.productRepository .createQueryBuilder("product") .leftJoinAndSelect('product.properties', 'property').getManyAndCount(); 或试试我在回复中的代码。
    • "usage.stockage" 是我的属性之一,我正在尝试获取具有特定属性的产品。 products.findByProperties([''usage.stockage"]) 之类的东西将返回所有具有确切属性的产品:['usage.stockage']。我不想返回带有属性字段的产品。我不知道我是否很清楚
    • 嗯,我想现在更清楚了。但是usageProperty 表的一列?或者usage.stockageProperty 表的名称(或其他)列中?第二个选项之后将是:this.productRepository.createQueryBuilder("product").leftJoin('product.properties', 'property').where("property.name = :propertyName", { propertyName: "usage.stockage"}).getManyAndCount();.
    • usage.stockage 是属性表中名为'key'的列的值,没错。这对一个人有用,但我想用数组测试,比如: this.productRepository.createQueryBuilder("product").leftJoin('product.properties', 'property').where("properties = :props", {道具:["usage.stockage", "usage.other"]}).getManyAndCount()
    猜你喜欢
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 2020-12-09
    • 2021-08-28
    • 2022-08-18
    • 2021-04-23
    • 2021-12-28
    • 2021-03-15
    相关资源
    最近更新 更多