Unknown column 'distinctAlias' when using find with pagination and sorting by a field from another table
See original GitHub issueIssue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo
TypeORM version:
[] latest [ ] @next [x] 0.3.0-rc.21
Steps to reproduce or a small repository showing the problem:
Model:
@Entity()
export default class Page {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
ownerId: string;
@Column()
type: string;
@OneToMany(
() => PageLocaleData,
(pageLocaleData: PageLocaleData) => pageLocaleData.page,
{ eager: false, cascade: true }
)
@ValidateNested()
localeData: PageLocaleData[];
}
@Entity()
@Unique(["page", "locale"])
export default class PageLocaleData {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column({ type: "enum", enum: Locale })
locale: Locale;
@ManyToOne(
() => Page,
(page: Page) => page.localeData,
{ onDelete: "CASCADE" }
)
page: Page;
@OneToMany(
() => PageData,
(pageData: PageData) => pageData.pageLocaleData,
{ cascade: true, eager: false }
)
@ValidateNested()
data: PageData[];
}
@Entity()
@Unique(["pageLocaleData", "status"])
export default class PageData {
@PrimaryGeneratedColumn("uuid")
id: string;
@ManyToOne(
() => PageLocaleData,
(pageLocaleData: PageLocaleData) => pageLocaleData.data,
{ onDelete: "CASCADE" }
)
pageLocaleData: PageLocaleData;
@Column({ type: "enum", enum: PageDataStatus })
status: PageDataStatus;
@Column({ default: "" })
title: string;
@Column({ default: 0 })
version: number;
@Column({ default: 0 })
updatedAt: number;
}
Query:
const pages = await getRepository(Page).find({
relations: {
localeData: {
data: true,
},
},
order: {
localeData: {
data: {
updatedAt: "DESC",
},
},
},
skip: 0,
take: 10,
});
Error:
{
code: 'ER_BAD_FIELD_ERROR',
errno: 1054,
sqlState: '42S22',
sqlMessage: "Unknown column 'distinctAlias.Page_localeData_data_updatedAt' in 'field list'"
}
SQL:
Typeorm generates such query:
SELECT DISTINCT `distinctAlias`.`Page_id` as "ids_Page_id", `distinctAlias`.`Page_localeData_data_updatedAt` FROM (SELECT `Page`.`id` AS `Page_id`, `Page`.`ownerId` AS `Page_ownerId`, `Page`.`type` AS `Page_type` FROM `page` `Page` INNER JOIN `page_locale_data` `Page_localeData` ON `Page_localeData`.`pageId`=`Page`.`id` INNER JOIN `project` `Page_project` ON `Page_project`.`id`=`Page`.`projectId` LEFT JOIN `page_data` `Page_localeData_data` ON `Page_localeData_data`.`pageLocaleDataId`=`Page_localeData`.`id`) `distinctAlias` ORDER BY `distinctAlias`.`Page_localeData_data_updatedAt` DESC, `Page_id` ASC LIMIT 25;
There is no Page_localeData_data.updatedAt in the selected fields for sub query and it’s not possible to add it.
This also not working:
const pages = await getRepository(Page).find({
select: {
id: true,
localeData: {
data: true,
},
},
relations: {
localeData: {
data: {
updatedAt: true,
},
},
},
order: {
localeData: {
data: {
updatedAt: "DESC",
},
},
},
skip: 0,
take: 10,
});
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Unknown column 'distinctAlias' when using findOneOrFail with ...
It may be a problem because the entity code does not exactly match the contents of the actual DB table. I solved it...
Read more >for SELECT DISTINCT, ORDER BY expressions must appear ...
I want to select users with their roles with pagination and filters. Error message: for SELECT DISTINCT, ORDER BY expressions must appear in ......
Read more >Paginating one table while sorting by a field from another table
How do I sort paginated data in cakephp 3 by a field from a different table.
Read more >Spring Boot Pagination and Sorting example - BezKoder
The Sort class provides sorting options for database queries with more flexibility in choosing single/multiple sort columns and directions ( ...
Read more >TIBCO JasperReports Server User Guide
To find out what you're licensed to use, or to upgrade your license, ... The Library table has two columns that refer to...
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
same issue here. so surprised that this well-reported issue was left without any comments from maintainers over two years ¯_(ツ)_/¯
Nevermind, that fixes nothing.
But adding
.addSelect(Object.keys(this.expressionMap.orderBys))after the .orderBy() does fix it.https://github.com/typeorm/typeorm/blob/c814b07653578f033808d01776293138751cc02e/src/query-builder/SelectQueryBuilder.ts#L3213-L3232