【问题标题】:Typeorm: Find entity with relations and where conditionTypeorm:查找具有关系和条件的实体
【发布时间】:2021-10-11 13:30:01
【问题描述】:

我正在使用 typeorm 并使用存储库来加载实体。我想查找房产及其活跃租户(is_active = true)

在 PropertyTenant 模型中,我有:

@ManyToOne(type => Property)
@JoinColumn({ name: 'property_id', referencedColumnName: 'id' })
property: Property;

在属性模型中,我有:

@OneToMany(
    () => PropertyTenant, (propertyTenant: PropertyTenant) => propertyTenant.property
)
propertyTenants: PropertyTenant[];

我尝试过这样的事情:

return await propertyRepository.findOne({
            where: {
                id: id,
                'propertyTenants.is_active': true
            },
            relations: [
                'location', 'location.city', 'location.city.state', 'propertyPurpose', 'propertyAmenities', 'propertyAmenities.amenity',
                'propertyTenants', 'propertyTenants.tenant', 'propertyType', 'propertyDocuments', 'propertyDocuments.documentType', 'propertyImages'
            ]
        });

它给出了错误:

No entity column \"propertyTenants.is_active\" was found.

我在文档中找不到任何内容。我不想使用查询构建,因为它返回原始数据,我需要对其进行处理。

有没有办法在@OneToMany 或@ManyToOne 中设置条件?

如何解决?

【问题讨论】:

    标签: node.js typeorm


    【解决方案1】:

    答案是使用QueryBuilder。您声明您不想使用它,因为它返回原始结果,但只有像 QueryBuilder.execute() 这样的函数才会返回原始结果。 .getOne().getMany() 将以实体格式返回结果,就像使用 Repository.findOne() 一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-03
      • 2019-04-24
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多