1
0
Fork 0
mirror of synced 2024-06-28 19:10:33 +12:00

Merge pull request #1807 from Budibase/fix/postgres-schema-issue

Postgres schema generation issue
This commit is contained in:
Michael Drury 2021-06-23 13:44:55 +01:00 committed by GitHub
commit f525ac03ac
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: {},
}