typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
34.22k stars 6.31k forks source link

typeorm requires {select:true} on entity field(if it's being used for `order by` clause ) when doing left join using `relations` options #9314

Open unique1o1 opened 2 years ago

unique1o1 commented 2 years ago

Expected Behavior

Left joining on any table should not require an order by column to exist in select clause

    This column is not mandatory in a select clause for this query to work!
                         ^
                         |
                         |
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;   <------- order by CustomerName

Actual Behavior

Typeorm requires a column(say "CustomerName") to exist on select clause for order by CustomerName to work when doing left join using relations options (relations:['Orders'])

{   ....
  "message": "column distinctAlias.Customers_CustomerName does not exist",
  "stack": "QueryFailedError: column distinctAlias.Customers_CustomerName does not exist\n    at PostgresQueryRunner.query "
   .....
}

PS: query works fine if the relations option is not provided.

Steps to Reproduce

  1. Create an Entity with a ManyToOne column and any column (say "CustomerName") with @Column({select:false}).
  2. Run a Find query {order: {CustomerName:"DESC"}, relations:["Orders"]}

My Environment

Dependency Version
Operating System PopOS 22.04
Node.js version v14.17.0
Typescript version 4.5.2
TypeORM version 0.3.6

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres 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
spanner no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

sthasanjay commented 1 year ago

facing the same issue here. Why are the authors ignoring this issue?