Cannot find alias for relation at ...
See original GitHub issueIssue Description
Using BaseEntity.find method and trying to filter by a property inside a related entity it throws the following error (this doesn’t happen if the property is the PK of the related entity):
Error: Cannot find alias for relation at currency
at _loop_3 (C:\...\node_modules\.pnpm\typeorm@0.2.38_ioredis@4.28.0+pg@8.7.1\src\query-builder\QueryBuilder.ts:954:27)
at SelectQueryBuilder.QueryBuilder.findColumnsForPropertyPath (C:\...\node_modules\.pnpm\typeorm@0.2.38_ioredis@4.28.0+pg@8.7.1\node_modules\typeorm\query-builder\QueryBuilder.js:827:27)
at SelectQueryBuilder.<anonymous> (C:\...\node_modules\.pnpm\typeorm@0.2.38_ioredis@4.28.0+pg@8.7.1\src\query-builder\QueryBuilder.ts:1069:68)
at step (C:\...\node_modules\.pnpm\tslib@2.3.1\node_modules\tslib\tslib.js:143:27)
at Object.next (C:\...\node_modules\.pnpm\tslib@2.3.1\node_modules\tslib\tslib.js:124:57)
at SelectQueryBuilder.QueryBuilder.getWhereCondition (C:\...\node_modules\.pnpm\typeorm@0.2.38_ioredis@4.28.0+pg@8.7.1\src\query-builder\QueryBuilder.ts:1208:80)
at SelectQueryBuilder.where (C:\...\node_modules\.pnpm\typeorm@0.2.38_ioredis@4.28.0+pg@8.7.1\src\query-builder\SelectQueryBuilder.ts:721:32)
at Function.FindOptionsUtils.applyOptionsToQueryBuilder (C:\...\node_modules\.pnpm\typeorm@0.2.38_ioredis@4.28.0+pg@8.7.1\src\find-options\FindOptionsUtils.ts:184:16)
at Function.FindOptionsUtils.applyFindManyOptionsOrConditionsToQueryBuilder (C:\...\node_modules\.pnpm\typeorm@0.2.38_ioredis@4.28.0+pg@8.7.1\src\find-options\FindOptionsUtils.ts:72:25)
at EntityManager.<anonymous> (C:\...\node_modules\.pnpm\typeorm@0.2.38_ioredis@4.28.0+pg@8.7.1\src\entity-manager\EntityManager.ts:677:26)
Expected Behavior
I expect it’s possible to find rows filtering by the value of any property of a related entity and not only by its PK.
Actual Behavior
It throws Error: Cannot find alias for relation at .... See above for the complete stacktrace.
Steps to Reproduce
Example:
Entity
@Entity('countries')
export class CountryEntity extends BaseEntity {
@PrimaryColumn({length: 2})
id: string;
...
@ManyToOne(() => CurrencyEntity, { nullable: true, eager: true })
currency: CurrencyEntity;
}
@Entity('currencies')
export class CurrencyEntity extends BaseEntity {
@PrimaryColumn()
id: string;
...
@Column()
fullName: string;
}
Lets say we want to find every single country which official currency’s fullName is United States dollar
const markets = await CountryEntity.find({
where: {
currency: {
fullName: 'United States dollar'
}
}
});
Result: Error: Cannot find alias for relation at currency.
If you search by PK currency.id instead of fullName, it would work:
const markets = await CountryEntity.find({
where: {
currency: {
id: 'USD'
}
}
});
Using QueryBuilder also works.
My Environment
| Dependency | Version |
|---|---|
| Operating System | Windows 10 |
| Node.js version | 16.3.0 |
| Typescript version | 4.4.4 |
| TypeORM version | 0.2.38 |
Additional Context
Relevant Database Driver(s)
| DB Type | Reproducible |
|---|---|
aurora-data-api |
no |
aurora-data-api-pg |
no |
better-sqlite3 |
no |
cockroachdb |
no |
cordova |
no |
expo |
no |
mongodb |
no |
mysql |
no |
nativescript |
no |
oracle |
no |
postgres |
yes |
react-native |
no |
sap |
no |
sqlite |
no |
sqlite-abstract |
no |
sqljs |
no |
sqlserver |
no |
Are you willing to resolve this issue by submitting a Pull Request?
- ✅ Yes, I have the time, but I don’t know how to start. I would need guidance.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:8
- Comments:13
Top Results From Across the Web
typeorm "\"user\" alias was not found. Maybe you forgot to join ...
Try to add a condition to your join: .leftJoinAndSelect('user.avatar', 'avatar', 'avatar.id = user.avatarId'). or/and inverse side to the ...
Read more >TypeORM: How to Use Column Alias when Querying Data
This succinct tutorial shows you how to use column aliases to assign temporary names to columns when selecting data with TypeORM.
Read more >Query with distinct sort and column alias produces error ...
I'm trying to use sql query on azure-databricks with distinct sort and aliases. SELECT DISTINCT album.ArtistId AS my_alias; FROM album ORDER ...
Read more >Find Options - typeorm - GitBook
All repository and manager .find* methods accept special options you can use to query ... relations - relations needs to be loaded with...
Read more >Custom aliases - dbt Developer Hub
When dbt runs a model, it will generally create a relation (either a table or a view ) in the database. By default,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I got the same error, I fixed by specifying
relationsin the conditionTry with:
Note: the downside is that the currency entity with by pulled also, it means, country’s currency will be populated.
Can we open PR for this @lucas-labs. I can submit the fix if needed