From efe1866dbed3f32188a18bc19f99d325834b972b Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 23 Jun 2021 12:44:54 +0100 Subject: [PATCH] Fixing an issue I found with postgres schema generator, it was applying all primary keys to all tables. --- .../server/scripts/integrations/postgres/init.sql | 10 +++++++++- packages/server/src/integrations/postgres.js | 13 ++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/server/scripts/integrations/postgres/init.sql b/packages/server/scripts/integrations/postgres/init.sql index baa7022f87..8d76f54a10 100644 --- a/packages/server/scripts/integrations/postgres/init.sql +++ b/packages/server/scripts/integrations/postgres/init.sql @@ -1,9 +1,17 @@ SELECT 'CREATE DATABASE main' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec CREATE TABLE Persons ( - PersonID int NOT NULL PRIMARY KEY, + PersonID INT NOT NULL PRIMARY KEY, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) ); +CREATE TABLE Tasks ( + TaskID INT NOT NULL PRIMARY KEY, + PersonID INT, + TaskName varchar(255), + CONSTRAINT fkPersons + FOREIGN KEY(PersonID) + REFERENCES Persons(PersonID) +); diff --git a/packages/server/src/integrations/postgres.js b/packages/server/src/integrations/postgres.js index 94cdad30ed..6266e6ca64 100644 --- a/packages/server/src/integrations/postgres.js +++ b/packages/server/src/integrations/postgres.js @@ -118,15 +118,18 @@ class PostgresIntegration extends Sql { * @param {*} datasourceId - datasourceId to fetch */ async buildSchema(datasourceId) { - let keys = [] + let tableKeys = {} try { const primaryKeysResponse = await this.client.query(this.PRIMARY_KEYS_SQL) for (let table of primaryKeysResponse.rows) { - keys.push(table.column_name || table.primary_key) + const tableName = table.table_name + if (!tableKeys[tableName]) { + tableKeys[tableName] = [] + } + tableKeys[tableName].push(table.column_name || table.primary_key) } } catch (err) { - // TODO: this try catch method isn't right - keys = ["id"] + tableKeys = {} } const columnsResponse = await this.client.query(this.COLUMNS_SQL) @@ -140,7 +143,7 @@ class PostgresIntegration extends Sql { if (!tables[tableName]) { tables[tableName] = { _id: buildExternalTableId(datasourceId, tableName), - primary: keys, + primary: tableKeys[tableName] || ["id"], name: tableName, schema: {}, }