Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix update table logic of when to remove PKs and add columns without assigning them a PK first

Joshen Lim8de348de37f1
diff --git a/studio/stores/pgmeta/MetaStore.ts b/studio/stores/pgmeta/MetaStore.ts
index c132c04c531..4a8516cf39a 100644
--- a/studio/stores/pgmeta/MetaStore.ts
+++ b/studio/stores/pgmeta/MetaStore.ts
@@ -2,7 +2,12 @@ import Papa from 'papaparse'
import { makeObservable, observable } from 'mobx'
import { find, isUndefined, isEqual, isEmpty, chunk, maxBy } from 'lodash'
import { Query } from '@supabase/grid'
-import { PostgresColumn, PostgresTable, PostgresRelationship } from '@supabase/postgres-meta'
+import {
+ PostgresColumn,
+ PostgresTable,
+ PostgresRelationship,
+ PostgresPrimaryKey,
+} from '@supabase/postgres-meta'
import { IS_PLATFORM, API_URL } from 'lib/constants'
import { post } from 'lib/common/fetch'
@@ -458,10 +463,24 @@ export default class MetaStore implements IMetaStore {
}
async updateTable(toastId: string, table: PostgresTable, payload: any, columns: ColumnField[]) {
- // Remove any primary key constraints first (we'll add it back later)
- if (table.primary_keys.length > 0) {
- const removePK = await this.removePrimaryKey(table.schema, table.name)
- if (removePK.error) throw removePK.error
+ // Prepare a check to see if primary keys to the tables were updated or not
+ const primaryKeyColumns = columns
+ .filter((column) => column.isPrimaryKey)
+ .map((column) => `"${column.name}"`)
+ const existingPrimaryKeyColumns = table.primary_keys.map(
+ (pk: PostgresPrimaryKey) => `"${pk.name}"`
+ )
+ const isPrimaryKeyUpdated = !isEqual(primaryKeyColumns, existingPrimaryKeyColumns)
+
+ if (isPrimaryKeyUpdated) {
+ // Remove any primary key constraints first (we'll add it back later)
+ // If we do it later, and if the user deleted a PK column, we'd need to do
+ // an additional check when removing PK if the column in the PK was removed
+ // So doing this one step earlier, lets us skip that additional check.
+ if (table.primary_keys.length > 0) {
+ const removePK = await this.removePrimaryKey(table.schema, table.name)
+ if (removePK.error) throw removePK.error
+ }
}
// Update the table
@@ -491,7 +510,12 @@ export default class MetaStore implements IMetaStore {
category: 'loading',
message: `Adding column ${column.name} to ${updatedTable.name}`,
})
- const columnPayload = generateCreateColumnPayload(updatedTable.id, column)
+ // Ensure that columns do not created as primary key first, cause the primary key will
+ // be added later on further down in the code
+ const columnPayload = generateCreateColumnPayload(updatedTable.id, {
+ ...column,
+ isPrimaryKey: false,
+ })
await this.createColumn(columnPayload, column.foreignKey)
} else {
const originalColumn = find(originalColumns, { id: column.id })
@@ -515,11 +539,8 @@ export default class MetaStore implements IMetaStore {
}
}
- // Add back the primary keys here
- const primaryKeyColumns = columns
- .filter((column) => column.isPrimaryKey)
- .map((column) => `"${column.name}"`)
- if (primaryKeyColumns.length > 0) {
+ // Then add back the primary keys again
+ if (isPrimaryKeyUpdated && primaryKeyColumns.length > 0) {
const primaryKeys = await this.addPrimaryKey(
updatedTable.schema,
updatedTable.name,