From 46daba1e6e22f5c600d1cf67d00f7412c9ae79dd Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sat, 9 Mar 2024 10:08:32 +0800 Subject: [PATCH] fix: infer postgres field (#3663) --- .../database/src/__tests__/view/view-inference.test.ts | 2 +- .../src/query-interface/postgres-query-interface.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/core/database/src/__tests__/view/view-inference.test.ts b/packages/core/database/src/__tests__/view/view-inference.test.ts index 8294bfba43..33b56de946 100644 --- a/packages/core/database/src/__tests__/view/view-inference.test.ts +++ b/packages/core/database/src/__tests__/view/view-inference.test.ts @@ -34,7 +34,7 @@ describe('view inference', function () { await db.sync(); - const viewName = 'user_posts'; + const viewName = 'test_view'; const dropViewSQL = `DROP VIEW IF EXISTS ${viewName}`; await db.sequelize.query(dropViewSQL); diff --git a/packages/core/database/src/query-interface/postgres-query-interface.ts b/packages/core/database/src/query-interface/postgres-query-interface.ts index 5e973dc385..183c4465cc 100644 --- a/packages/core/database/src/query-interface/postgres-query-interface.ts +++ b/packages/core/database/src/query-interface/postgres-query-interface.ts @@ -153,10 +153,14 @@ export default class PostgresQueryInterface extends QueryInterface { // handle column alias const from = ast[0].from; - const findAs = from.find((from) => from.as === columnExprTable); + if (columnExprTable === null && column.expr.type === 'column_ref') { + columnExprTable = from[0].table; + } else { + const findAs = from.find((from) => from.as === columnExprTable); - if (findAs) { - columnExprTable = findAs.table; + if (findAs) { + columnExprTable = findAs.table; + } } return columnUsage.column_name === column.expr.column && columnUsage.table_name === columnExprTable;