[英]Typeorm: Find entity with relations and where condition
我正在使用 typeorm 並使用存儲庫加載實體。 我想找到房產及其活躍租戶(is_active = true)
在 PropertyTenant model 中,我有:
@ManyToOne(type => Property)
@JoinColumn({ name: 'property_id', referencedColumnName: 'id' })
property: Property;
在財產 model 中,我有:
@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?
如何解決這個問題?
答案是使用QueryBuilder
。 你 state 你不想使用它,因為它返回原始結果,但只有像QueryBuilder.execute()
這樣的函數才會返回原始結果。 .getOne()
和.getMany()
將以實體格式返回結果,就像您使用Repository.findOne()
獲得的結果一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.