The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Add option to make a column a primary key in column editor
Joshen Lim796692c7a690
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsxindex 4a5a167af01..201ab855edf 100644--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx@@ -122,7 +122,7 @@ const ColumnEditor: FC<Props> = ({if (isEmpty(errors)) {const payload = isNewRecord? generateCreateColumnPayload(selectedTable.id, columnFields)- : generateUpdateColumnPayload(column!, columnFields)+ : generateUpdateColumnPayload(column!, selectedTable, columnFields)const foreignKey = columnFields.foreignKey? { ...columnFields.foreignKey, source_column_name: columnFields.name }: undefined@@ -177,6 +177,16 @@ const ColumnEditor: FC<Props> = ({value={columnFields?.comment ?? ''}onChange={(event: any) => onUpdateField({ comment: event.target.value })}/>+ <div className="grid grid-cols-12 gap-4">+ <div className="col-span-12 md:col-span-8 md:col-start-5">+ <Checkbox+ label="Is Primary Key"+ description="A primary key indicates that a column or group of columns can be used as a unique identifier for rows in the table."+ checked={columnFields?.isPrimaryKey ?? false}+ onChange={() => onUpdateField({ isPrimaryKey: !columnFields?.isPrimaryKey })}+ />+ </div>+ </div></div></SidePanel.Content>@@ -184,18 +194,6 @@ const ColumnEditor: FC<Props> = ({<SidePanel.Content><div className="space-y-10 py-6">- {isNewRecord && !hasPrimaryKey && (- <div className="grid grid-cols-12 gap-4">- <div className="col-span-12 md:col-span-8 md:col-start-5">- <Checkbox- label={hasPrimaryKey ? 'Add to composite primary key' : 'Is Primary Key'}- description="A primary key indicates that a column or group of columns can be used as a unique identifier for rows in the table."- checked={columnFields?.isPrimaryKey ?? false}- onChange={() => onUpdateField({ isPrimaryKey: !columnFields?.isPrimaryKey })}- />- </div>- </div>- )}<ColumnForeignKeycolumn={columnFields}originalForeignKey={originalForeignKey}@@ -216,8 +214,12 @@ const ColumnEditor: FC<Props> = ({/><div className="text-sm grid md:grid-cols-12 md:gap-x-4 ">- <p className='text-scale-1100 col-span-7 col-start-5 -mt-4'>- <a href="https://supabase.com/docs/guides/database/tables#data-types" target="_blank" className="underline inline-block gap-1 p-2 rounded-sm">+ <p className="text-scale-1100 col-span-7 col-start-5 -mt-4">+ <a+ href="https://supabase.com/docs/guides/database/tables#data-types"+ target="_blank"+ className="underline inline-block gap-1 p-2 rounded-sm"+ >Learn more about data types</a></p>diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.tsindex 7385ef0a5a1..fd68f6d2fa5 100644--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts@@ -103,8 +103,13 @@ export const generateCreateColumnPayload = (export const generateUpdateColumnPayload = (originalColumn: PostgresColumn,+ table: PostgresTable,field: ColumnField): Partial<UpdateColumnPayload> => {+ // @ts-ignore+ const primaryKeyColumns = table.primary_keys.map((key) => key.name)+ const isOriginallyPrimaryKey = primaryKeyColumns.includes(originalColumn.name)+// Only append the properties which are getting updatedconst defaultValue = field.defaultValueconst type = field.isArray ? `${field.format}[]` : field.format@@ -136,6 +141,9 @@ export const generateUpdateColumnPayload = (if (!isEqual(originalColumn.is_unique, field.isUnique)) {payload.isUnique = field.isUnique}+ if (!isEqual(isOriginallyPrimaryKey, field.isPrimaryKey)) {+ payload.isPrimaryKey = field.isPrimaryKey+ }return payload}diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsxindex 8664cbfb1c3..237b887165e 100644--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx@@ -120,7 +120,11 @@ const SidePanelEditor: FC<Props> = ({resolve: any) => {const response = isNewRecord- ? await meta.createColumn(payload as CreateColumnPayload, foreignKey)+ ? await meta.createColumn(+ payload as CreateColumnPayload,+ selectedTable as PostgresTable,+ foreignKey+ ): await meta.updateColumn(configuration.columnId as string,payload as UpdateColumnPayload,diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.tsindex 0a32830fa62..b4d0338e14f 100644--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts@@ -25,6 +25,7 @@ export interface UpdateColumnPayload {isIdentity?: booleanisNullable?: booleanisUnique?: boolean+ isPrimaryKey?: booleanidentityGeneration?: 'BY DEFAULT' | 'ALWAYS'}diff --git a/studio/stores/pgmeta/MetaStore.ts b/studio/stores/pgmeta/MetaStore.tsindex d36aa3d7a3f..cfec8459815 100644--- a/studio/stores/pgmeta/MetaStore.ts+++ b/studio/stores/pgmeta/MetaStore.ts@@ -84,12 +84,17 @@ export interface IMetaStore {/** The methods below involve several contexts due to the UI flow of the* dashboard and hence do not sit within their own stores */- createColumn: (payload: CreateColumnPayload, foreignKey?: Partial<PostgresRelationship>) => any+ createColumn: (+ payload: CreateColumnPayload,+ selectedTable: PostgresTable,+ foreignKey?: Partial<PostgresRelationship>+ ) => anyupdateColumn: (id: string,payload: UpdateColumnPayload,selectedTable: PostgresTable,- foreignKey?: Partial<PostgresRelationship>+ foreignKey?: Partial<PostgresRelationship>,+ skipPKCreation?: boolean) => anyduplicateTable: (payload: any,@@ -314,11 +319,34 @@ export default class MetaStore implements IMetaStore {}}- async createColumn(payload: CreateColumnPayload, foreignKey?: Partial<PostgresRelationship>) {+ async createColumn(+ payload: CreateColumnPayload,+ selectedTable: PostgresTable,+ foreignKey?: Partial<PostgresRelationship>+ ) {try {- const column: any = await this.columns.create(payload)+ // Once pg-meta supports composite keys, we can remove this logic+ const { isPrimaryKey, ...formattedPayload } = payload++ const column: any = await this.columns.create(formattedPayload)if (column.error) throw column.error+ // Firing createColumn in createTable will bypass this block+ if (isPrimaryKey) {+ // Same logic in createTable: Remove any primary key constraints first (we'll add it back later)+ // @ts-ignore+ const existingPrimaryKeys = selectedTable.primary_keys.map((x) => x.name)++ if (existingPrimaryKeys.length > 0) {+ const removePK = await this.removePrimaryKey(column.schema, column.table)+ if (removePK.error) throw removePK.error+ }++ const primaryKeyColumns = existingPrimaryKeys.concat([column.name])+ const addPK = await this.addPrimaryKey(column.schema, column.table, primaryKeyColumns)+ if (addPK.error) throw addPK.error+ }+if (!isUndefined(foreignKey)) {const relation: any = await this.addForeignKey(foreignKey)if (relation.error) throw relation.error@@ -332,10 +360,12 @@ export default class MetaStore implements IMetaStore {id: string,payload: UpdateColumnPayload,selectedTable: PostgresTable,- foreignKey?: Partial<PostgresRelationship>+ foreignKey?: Partial<PostgresRelationship>,+ skipPKCreation?: boolean) {try {- const column: any = await this.columns.update(id, payload)+ const { isPrimaryKey, ...formattedPayload } = payload+ const column: any = await this.columns.update(id, formattedPayload)if (column.error) throw column.errorconst originalColumn = find(selectedTable.columns, { id })@@ -343,6 +373,21 @@ export default class MetaStore implements IMetaStore {source_column_name: originalColumn!.name,})+ if (!skipPKCreation && isPrimaryKey !== undefined) {+ const existingPrimaryKeys = selectedTable.primary_keys.map((x) => x.name)++ // Primary key is getting updated for the column+ if (existingPrimaryKeys.length > 0) {+ 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+ }+// For updating of foreign key relationship, we remove the original one by default// Then just add whatever was in foreignKey - simplicity over trying to derive whether to update or notif (!isUndefined(existingForeignKey)) {@@ -623,11 +668,11 @@ export default class MetaStore implements IMetaStore {...column,isPrimaryKey: false,})- await this.createColumn(columnPayload, column.foreignKey)+ await this.createColumn(columnPayload, updatedTable, column.foreignKey)} else {const originalColumn = find(originalColumns, { id: column.id })if (originalColumn) {- const columnPayload = generateUpdateColumnPayload(originalColumn, column)+ const columnPayload = generateUpdateColumnPayload(originalColumn, updatedTable, column)const originalForeignKey = find(table.relationships, {source_schema: originalColumn.schema,source_table_name: originalColumn.table,@@ -640,11 +685,13 @@ export default class MetaStore implements IMetaStore {category: 'loading',message: `Updating column ${column.name} from ${updatedTable.name}`,})+ const skipPKCreation = trueconst res: any = await this.updateColumn(column.id,columnPayload,updatedTable,- column.foreignKey+ column.foreignKey,+ skipPKCreation)if (res?.error) {hasError = true