From 9c54876f434cb544c73e1bcba65a03b8ef466f5f Mon Sep 17 00:00:00 2001 From: gitstart Date: Wed, 12 Apr 2023 16:58:12 +0000 Subject: [PATCH] fix: column primary key toggling errors Co-authored-by: phunguyenmurcul <51897872+phunguyenmurcul@users.noreply.github.com> --- studio/stores/pgmeta/MetaStore.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/studio/stores/pgmeta/MetaStore.ts b/studio/stores/pgmeta/MetaStore.ts index 92ac9067ebd..1b606082d24 100644 --- a/studio/stores/pgmeta/MetaStore.ts +++ b/studio/stores/pgmeta/MetaStore.ts @@ -288,8 +288,8 @@ export default class MetaStore implements IMetaStore { } async addPrimaryKey(schema: string, table: string, columns: string[]) { - const primaryKeyColumns = columns.join(',') - const query = `ALTER TABLE "${schema}"."${table}" ADD PRIMARY KEY (${primaryKeyColumns})` + const primaryKeyColumns = columns.join('","') + const query = `ALTER TABLE "${schema}"."${table}" ADD PRIMARY KEY ("${primaryKeyColumns}")` return await this.query(query) } @@ -501,11 +501,15 @@ export default class MetaStore implements IMetaStore { const removePK = await this.removePrimaryKey(column.schema, column.table) if (removePK.error) throw removePK.error } + const primaryKeyColumns = isPrimaryKey ? existingPrimaryKeys.concat([column.name]) : existingPrimaryKeys.filter((x) => x !== column.name) - const addPK = await this.addPrimaryKey(column.schema, column.table, primaryKeyColumns) - if (addPK.error) throw addPK.error + + if (primaryKeyColumns.length) { + const addPK = await this.addPrimaryKey(column.schema, column.table, primaryKeyColumns) + if (addPK.error) throw addPK.error + } } // For updating of foreign key relationship, we remove the original one by default