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' SELECT 'CREATE DATABASE main'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec
CREATE TABLE Persons ( CREATE TABLE Persons (
PersonID int NOT NULL PRIMARY KEY, PersonID INT NOT NULL PRIMARY KEY,
LastName varchar(255), LastName varchar(255),
FirstName varchar(255), FirstName varchar(255),
Address varchar(255), Address varchar(255),
City 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 * @param {*} datasourceId - datasourceId to fetch
*/ */
async buildSchema(datasourceId) { async buildSchema(datasourceId) {
let keys = [] let tableKeys = {}
try { try {
const primaryKeysResponse = await this.client.query(this.PRIMARY_KEYS_SQL) const primaryKeysResponse = await this.client.query(this.PRIMARY_KEYS_SQL)
for (let table of primaryKeysResponse.rows) { 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) { } catch (err) {
// TODO: this try catch method isn't right tableKeys = {}
keys = ["id"]
} }
const columnsResponse = await this.client.query(this.COLUMNS_SQL) const columnsResponse = await this.client.query(this.COLUMNS_SQL)
@ -140,7 +143,7 @@ class PostgresIntegration extends Sql {
if (!tables[tableName]) { if (!tables[tableName]) {
tables[tableName] = { tables[tableName] = {
_id: buildExternalTableId(datasourceId, tableName), _id: buildExternalTableId(datasourceId, tableName),
primary: keys, primary: tableKeys[tableName] || ["id"],
name: tableName, name: tableName,
schema: {}, schema: {},
} }