Fixing an issue I found with postgres schema generator, it was applying all primary keys to all tables.

This commit is contained in:
mike12345567 2021-06-23 12:44:54 +01:00
parent e141a17e87
commit b3f3826aa6
2 changed files with 17 additions and 6 deletions

View File

@ -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)
);

View File

@ -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: {},
}