From 86abc251d9e5c98d677710af6f596349efd25a02 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sun, 2 Apr 2023 22:50:55 +0800 Subject: [PATCH] fix: get pg view def (#1641) * fix: get pg view def * chore: console.log --- .../postgres-query-interface.ts | 21 +++-- .../__tests__/collections.repository.test.ts | 76 ++++++++++++++++++- 2 files changed, 89 insertions(+), 8 deletions(-) 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 e730adc410..9d5580c9d1 100644 --- a/packages/core/database/src/query-interface/postgres-query-interface.ts +++ b/packages/core/database/src/query-interface/postgres-query-interface.ts @@ -56,9 +56,7 @@ export default class PostgresQueryInterface extends QueryInterface { const viewDefQuery = await this.db.sequelize.query( ` - SELECT viewname, definition -FROM pg_views -WHERE schemaname = '${schema}' AND viewname = '${viewName}'; + select pg_get_viewdef(format('%I.%I', '${schema}', '${viewName}')::regclass, true) as definition `, { type: 'SELECT' }, ); @@ -71,10 +69,19 @@ WHERE schemaname = '${schema}' AND viewname = '${viewName}'; const usages = columns .map((column) => { const fieldAlias = column.as || column.expr.column; - const columnUsage = columnUsages.find( - (columnUsage) => - columnUsage.column_name === column.expr.column && columnUsage.table_name === column.expr.table, - ); + const columnUsage = columnUsages.find((columnUsage) => { + let columnExprTable = column.expr.table; + + // handle column alias + const from = ast[0].from; + const findAs = from.find((from) => from.as === columnExprTable); + + if (findAs) { + columnExprTable = findAs.table; + } + + return columnUsage.column_name === column.expr.column && columnUsage.table_name === columnExprTable; + }); return [ fieldAlias, diff --git a/packages/plugins/collection-manager/src/__tests__/collections.repository.test.ts b/packages/plugins/collection-manager/src/__tests__/collections.repository.test.ts index bc36995292..e48f8e108d 100644 --- a/packages/plugins/collection-manager/src/__tests__/collections.repository.test.ts +++ b/packages/plugins/collection-manager/src/__tests__/collections.repository.test.ts @@ -1,4 +1,4 @@ -import Database, { Collection as DBCollection } from '@nocobase/database'; +import Database, { Collection as DBCollection, HasManyRepository } from '@nocobase/database'; import Application from '@nocobase/server'; import { createApp } from '.'; import CollectionManagerPlugin, { CollectionRepository } from '@nocobase/plugin-collection-manager'; @@ -605,4 +605,78 @@ describe('collections repository', () => { expect(err).toBeFalsy(); }); + + it('should update association field', async () => { + const A = await Collection.repository.create({ + values: { + name: 'a', + fields: [ + { + name: 'title', + type: 'string', + }, + { + name: 'bs', + type: 'hasMany', + uiSchema: { + title: 'bs-title', + }, + }, + ], + }, + context: {}, + }); + + const B = await Collection.repository.create({ + values: { + name: 'b', + fields: [ + { + type: 'string', + name: 'title', + }, + { + type: 'belongsTo', + name: 'a', + }, + ], + uiSchema: { + title: 'b-title', + }, + }, + context: {}, + }); + + const C = await Collection.repository.create({ + values: { + name: 'c', + fields: [ + { + type: 'string', + name: 'title', + }, + { + type: 'belongsTo', + name: 'a', + }, + ], + uiSchema: { + title: 'c-title', + }, + }, + context: {}, + }); + + await db.sync(); + + await db.getRepository('collections.fields', 'c').update({ + filterByTk: 'a', + values: { + key: C.key, + uiSchema: { + title: 'c-hello-world', + }, + }, + }); + }); });