typeorm: Error: Cannot get entity metadata for the given alias
Issue type:
[x] question [ ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Steps to reproduce or a small repository showing the problem: Hi. I’m having an issue with retrieving documents from a nested FROM-clause like this:
let query = getConnection().createQueryBuilder()
.from((qb) => {
return qb
.from(A, "a")
.limit(1);
}
, "alias")
And get the error
Cannot get entity metadata for the given alias "alias".
Also, if I try to add another from:
.from(A, "b")
the query works, but the second FROM doesn’t replace the first one, as specified in the docs. How can I do this the right way? Thanks
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 76
- Comments: 48 (2 by maintainers)
Commits related to this issue
- fix: remove the main alias when using from() Closes: #4015 — committed to ancyrweb/typeorm by deleted user 5 years ago
Works only with getRawMany()
For anyone having this issue in the same boat as me where I was camelCasing my entity name, changing to snake_case corrected the problem for me.
For anyone interested I was having this issue when using
getManybut I fixed it by usinggetRawMany()insteadIdeally I’d like to use getMany since it’s harder to get the entity mapped with the raw results
Try this hack:
Hope it will help someone
@pleerock A query builder that can’t nest queries is like a car with two wheels. Is this going to get fixed anytime soon?
@pleerock -> OrderBy with an alias name breaks our queries that have subqueries:
Using the info in this PR and this comment, I was able to use my subquery with entity mappings. It works with
getMany.I also had to emulate the way TypeORM was generating selects for my base entity (i.e.
Entity.idbecomesEntity_id).It looks like this:
Same problem here.
For the moment I am running the raw query/params as a workaround as I do not need any special typeorm methods for running the query.
same problem
Same issue. I suggest use
repository.query("SQL");Hey everyone. If anyone still have this issue in 2024, I think I found a proper solution (at least for my case). I tried to use sub query in leftJoin and got this error. So, basically you need to assign a metadata manually to alias in the main query. Here is the example (I wanted to use LATERAL, which is not part of Typeorm functionality) :
Maybe it will help someone
Welcome! 🤙
So, it’s 2023 and the issue still exists. More over, with some workarounds it works in a wrong way. Is there any ETA on resolving this issue?
@PadraigGalvin Thanks It worked
Does anybody fixed this issue?. Could anybody post an explanatory Answer to this problem please.
Same here. Please infer the meta from the subselect. The SelectQueryBuilder when
entityTarget instanceof Functionknows the needed metaSame problem
Same problem.
same problem here, any one fixed it?
Same problem.
Same problem, please help.