TypeORM,基于关系属性的查询实体 [英] TypeORM, Query entity based on relation property
问题描述
我想根据相关属性查询实体,例如:
I would like to query an entity based on a related property, for instance:
const x = await repo.findOne({ name: 'foo', parent: { name: 'foo' }});
但是当我通过其相关的 parent
but it aways returns a null when I query by its related parent
我已经添加:relations: ['parent']
,已经将关系设置为{eager:true}
I alread added : relations: ['parent']
, already set relation as {eager:true}
当我通过 parent: {id: X}
查询时,它起作用了.但我必须按它的名字查询.
When I Query by parent: {id: X}
it works. but I must query by its name.
我应该怎么做才能让这个查询在 TypeORM 中工作
What should I do to get this query working in TypeORM
类似于:
select * from entity internal join parent ... where entity.name = 'foo' and parent.name = 'foo'
select * from entity inner join parent ... where entity.name = 'foo' and parent.name = 'foo'
推荐答案
find
/findOne
不允许通过嵌套关系属性进行过滤.使用 QueryBuilder
代替
find
/findOne
doesn't allow filtering by nested relation properties. Go for QueryBuilder
instead with something like
const x = await repo.createQueryBuilder("foo")
.innerJoinAndSelect("foo.parent", "parent")
.where("parent.name = :name", { name })
.getOne()
在此处查看类似问题.
这篇关于TypeORM,基于关系属性的查询实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!