You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error: Cannot get entity metadata for the given alias #4015
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
Reacted by Toni Villena, Anthony Cyrille, alex-outsourci, PomanoB, Christian Kink, MOTORIST, Alberto Naperi Jr., Matthew Miller, Mihai Tomescu, armin-th and 59 more
Reacted by Teddy Toussaint, fuhua-zhang, Nicolò, Lukas and Saul Flores
Reacted by Dmytro Brahinets, Vijay Koushik, S., Lukas, Angel Diaz and Mohammad Tourjoman
Essentially, I am trying to wrap an existing query in another query to operate on the results and I am getting the same error. When I output the query string and execute it, it runs as expected but not in TypeORM.
Reacted by Peter Tanugraha, Toni Villena, Idongesit Ntuk, Mykhailo Zhuk, Irfan Hanandra, Pádraig Galvin, Dev , Owen Hagerty, Matt, Chetan Khandla and 17 more
Reacted by Matt, Chetan Khandla, CHAHIN, Rohit Singhal, osimple, hun Park, アニップ and Reculos Gerbi Neto
funny fact getRawMany() works some times
to don't get in to the query details just made it shorter and replace some values, but i think the point is clear
await this.MyRepo.metadata.connection.createQueryBuilder()
.select(`DISTINCT some-value`)
.from(`(${someQueryObject.getQuery()})` , 'subQ')
.setParameters(someQueryObject.getParameters())
.getRawMany()
.then(result => {
//this works just fine and result has the selected data
});
// thing is I actually need to total count so would like to do
const q = this.MyRepo.metadata.connection.createQueryBuilder()
.select(`DISTINCT some-value`)
.from(`(${someQueryObject.getQuery()})` , 'subQ')
.setParameters(someQueryObject.getParameters());
const total = await q.getCount(); //so far so good
await q.getRawMany()
.then(res => {
//and here is the error
});
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) :
//ADD METADATA for productCollection alias (without it shows typeorm error 'Cannot get entity metadata for the given alias')constalias=newAlias();alias.type='join'alias.name='productCollection'alias.metadata=query.connection.getMetadata(ProductCollection)query.expressionMap.aliases.push(alias);//ADD LEFT JOIN with LATERAL construction by overriding getQuery function (HACK)query.leftJoin((qb)=>{qb.getQuery=()=>'LATERAL (SELECT * FROM "product_collection" "product_collection" WHERE "product_collection"."productId" = "product"."id" LIMIT 1)';qb.setParameters({});returnqb;},'productCollection','TRUE')
I am having a very similar issue, but with getCount(). My query is similar to the original one in the first post: using subQuery inside from(), then using getRawMany() for the results with limit and also getCount() to get all results possible. But the getCount() throws this given alias error. Using only getRawMany() works as expected.
Congrats, it's 2025 and we keep on this issue. This is a 6-year issue! We ran into this issue everytime we need to use a subquery!
not sure how far in the triage of all the old stuff is, but it could be that is planned. As you can see from the last 2 recent releases things are moving on.
Encountering the same issue using a subquery in .leftJoin
qb.leftJoin((subQuery)=>subQuery.from(Entity,'et').select(['et.id','et.createdAt',]).orderBy('et.createdAt','ASC'),// also tried only using createdAt instead of et.createdAt'entity','entity.parentId = parent.id',)
Activity
tonivj5 commentedon Jul 31, 2019
I have the same problem, any help? 😢
fix: remove the main alias when using from()
0b3r commentedon Aug 30, 2019
Same issue here. I tried doing the exact thing in the documentation (with my own schema) and it is not working.
Essentially, I am trying to wrap an existing query in another query to operate on the results and I am getting the same error. When I output the query string and execute it, it runs as expected but not in TypeORM.
Has any one done this?
yescsabi commentedon Sep 10, 2019
armin-th commentedon Nov 14, 2019
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.
xmsz commentedon Nov 20, 2019
jimmdd commentedon Jan 2, 2020
sebapatr96 commentedon Jan 16, 2020
dlarchikov commentedon Jan 26, 2020
seandearnaley commentedon Feb 6, 2020
volovodenko commentedon Feb 18, 2020
Works only with getRawMany()
fullofcaffeine commentedon Mar 15, 2020
abdullah2993 commentedon Mar 19, 2020
erolkoseoglu commentedon Mar 31, 2020
zpwebbear commentedon Apr 14, 2020
I have the same problem
30 remaining items
celxl commentedon Jun 2, 2023
funny fact getRawMany() works some times
to don't get in to the query details just made it shorter and replace some values, but i think the point is clear
vtereshyn commentedon Oct 4, 2023
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?
nrathi commentedon Oct 12, 2023
+1
knovu commentedon Dec 15, 2023
@pleerock -> OrderBy with an alias name breaks our queries that have subqueries:
kawamura-vivit commentedon Feb 20, 2024
Thanks knovu!! It solved the same problem I had.
knovu commentedon Feb 20, 2024
Welcome! 🤙
parrotkesha commentedon Mar 13, 2024
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
kasir-barati commentedon Jan 1, 2025
Happy New Year TypeORM, its time for this issue to be solved as part of the Christmas gifts Santa Claus were supposed to give us all 🥹.
trannhan0810 commentedon Mar 16, 2025
Maybe this PR will help , @imnotjames
Creatium commentedon Mar 20, 2025
I am having a very similar issue, but with getCount(). My query is similar to the original one in the first post: using subQuery inside from(), then using getRawMany() for the results with limit and also getCount() to get all results possible. But the getCount() throws this given alias error. Using only getRawMany() works as expected.
henriquetroiano commentedon Apr 5, 2025
Congrats, it's 2025 and we keep on this issue. This is a 6-year issue!
We ran into this issue everytime we need to use a subquery!
hinogi commentedon Apr 6, 2025
not sure how far in the triage of all the old stuff is, but it could be that is planned. As you can see from the last 2 recent releases things are moving on.
truesteps commentedon May 9, 2025
Encountering the same issue using a subquery in
.leftJoin