The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Fix: Make the FK editor faster when loading tables (#40259)
Ali Waseemac6db4166276
diff --git a/apps/studio/components/grid/components/formatter/ForeignKeyFormatter.tsx b/apps/studio/components/grid/components/formatter/ForeignKeyFormatter.tsxindex ad4a8d06546..c960551a749 100644--- a/apps/studio/components/grid/components/formatter/ForeignKeyFormatter.tsx+++ b/apps/studio/components/grid/components/formatter/ForeignKeyFormatter.tsx@@ -1,12 +1,14 @@+import type { PostgresTable } from '@supabase/postgres-meta'import { ArrowRight } from 'lucide-react'import type { PropsWithChildren } from 'react'import type { RenderCellProps } from 'react-data-grid'import { convertByteaToHex } from 'components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils'import { ButtonTooltip } from 'components/ui/ButtonTooltip'+import ShimmeringLoader from 'components/ui/ShimmeringLoader'import { useTableEditorQuery } from 'data/table-editor/table-editor-query'import { isTableLike } from 'data/table-editor/table-editor-types'-import { useTablesQuery } from 'data/tables/tables-query'+import { useTablesQuery as useTableRetrieveQuery } from 'data/tables/table-retrieve-query'import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'import { Popover_Shadcn_, PopoverContent_Shadcn_, PopoverTrigger_Shadcn_ } from 'ui'import type { SupaRow } from '../../types'@@ -21,7 +23,7 @@ export const ForeignKeyFormatter = (props: Props) => {const { tableId, row, column } = propsconst { data: project } = useSelectedProjectQuery()- const { data } = useTableEditorQuery({+ const { data, isLoading } = useTableEditorQuery({projectRef: project?.ref,connectionString: project?.connectionString,id: tableId,@@ -35,17 +37,22 @@ export const ForeignKeyFormatter = (props: Props) => {r.source_table_name === selectedTable?.name &&r.source_column_name === column.name)- const { data: tables } = useTablesQuery({- projectRef: project?.ref,- includeColumns: true,- connectionString: project?.connectionString,- schema: relationship?.target_table_schema,- })- const targetTable = tables?.find(- (table) =>- table.schema === relationship?.target_table_schema &&- table.name === relationship.target_table_name- )++ const { data: targetTable, isLoading: isLoadingTargetTable } =+ useTableRetrieveQuery<PostgresTable>(+ {+ projectRef: project?.ref,+ connectionString: project?.connectionString,+ schema: relationship?.target_table_schema ?? '',+ name: relationship?.target_table_name ?? '',+ },+ {+ enabled:+ !!project?.ref &&+ !!relationship?.target_table_schema &&+ !!relationship?.target_table_name,+ }+ )const value = row[column.key]const formattedValue =@@ -56,25 +63,39 @@ export const ForeignKeyFormatter = (props: Props) => {<span className="sb-grid-foreign-key-formatter__text">{formattedValue === null ? <NullValue /> : formattedValue}</span>- {relationship !== undefined && targetTable !== undefined && formattedValue !== null && (- <Popover_Shadcn_>- <PopoverTrigger_Shadcn_ asChild>- <ButtonTooltip- type="default"- className="w-6 h-6"- icon={<ArrowRight />}- onClick={(e) => e.stopPropagation()}- tooltip={{ content: { side: 'bottom', text: 'View referencing record' } }}- />- </PopoverTrigger_Shadcn_>- <PopoverContent_Shadcn_ portal align="end" className="p-0 w-96">- <ReferenceRecordPeek- table={targetTable}- column={relationship.target_column_name}- value={formattedValue}- />- </PopoverContent_Shadcn_>- </Popover_Shadcn_>+ {isLoading && formattedValue !== null && (+ <div className="w-6 h-6 flex items-center justify-center">+ <ShimmeringLoader className="w-4 h-4" />+ </div>+ )}+ {!isLoading && relationship !== undefined && formattedValue !== null && (+ <>+ {isLoadingTargetTable && (+ <div className="w-6 h-6 flex items-center justify-center">+ <ShimmeringLoader className="w-4 h-4" />+ </div>+ )}+ {!isLoadingTargetTable && targetTable !== undefined && (+ <Popover_Shadcn_>+ <PopoverTrigger_Shadcn_ asChild>+ <ButtonTooltip+ type="default"+ className="w-6 h-6"+ icon={<ArrowRight />}+ onClick={(e) => e.stopPropagation()}+ tooltip={{ content: { side: 'bottom', text: 'View referencing record' } }}+ />+ </PopoverTrigger_Shadcn_>+ <PopoverContent_Shadcn_ portal align="end" className="p-0 w-96">+ <ReferenceRecordPeek+ table={targetTable}+ column={relationship.target_column_name}+ value={formattedValue}+ />+ </PopoverContent_Shadcn_>+ </Popover_Shadcn_>+ )}+ </>)}</div>)diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsxindex 9d6819c1224..22fbdeb0b26 100644--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx@@ -47,7 +47,7 @@ const ColumnForeignKey = ({return {id: c.id,name: c.name,- format: column.format || c.format,+ format: c.format || column.format,isNewColumn: false,}})diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsxindex 810f962e953..be7eee6e8d1 100644--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx@@ -1,6 +1,6 @@import type { PostgresTable } from '@supabase/postgres-meta'import { sortBy } from 'lodash'-import { ArrowRight, Database, HelpCircle, Table, X } from 'lucide-react'+import { ArrowRight, Database, HelpCircle, Loader2, Table, X } from 'lucide-react'import { Fragment, useEffect, useState } from 'react'import {AlertDescription_Shadcn_,@@ -16,6 +16,7 @@ import InformationBox from 'components/ui/InformationBox'import { FOREIGN_KEY_CASCADE_ACTION } from 'data/database/database-query-constants'import { useSchemasQuery } from 'data/database/schemas-query'import { useTablesQuery } from 'data/tables/tables-query'+import { useTablesQuery as useTableRetrieveQuery } from 'data/tables/table-retrieve-query'import { useQuerySchemaState } from 'hooks/misc/useSchemaQueryState'import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'import { DOCS_URL } from 'lib/constants'@@ -69,16 +70,27 @@ export const ForeignKeySelector = ({projectRef: project?.ref,connectionString: project?.connectionString,})- const { data: tables } = useTablesQuery<PostgresTable[] | undefined>({+ const { data: tables } = useTablesQuery({projectRef: project?.ref,connectionString: project?.connectionString,schema: fk.schema,- includeColumns: true,+ includeColumns: false,})- const selectedTable = (tables ?? []).find((x) => x.name === fk.table && x.schema === fk.schema)+ const { data: selectedTable, isLoading: isLoadingSelectedTable } =+ useTableRetrieveQuery<PostgresTable>(+ {+ projectRef: project?.ref,+ connectionString: project?.connectionString,+ schema: fk.schema,+ name: fk.table,+ },+ {+ enabled: !!project?.ref && !!fk.schema && !!fk.table,+ }+ )- const disableApply = selectedTable === undefined || hasTypeErrors+ const disableApply = isLoadingSelectedTable || selectedTable === undefined || hasTypeErrorsconst updateSelectedSchema = (schema: string) => {const updatedFk = { ...EMPTY_STATE, id: fk.id, schema }@@ -258,6 +270,7 @@ export const ForeignKeySelector = ({label="Select a table to reference to"value={selectedTable?.id ?? 1}onChange={(value: string) => updateSelectedTable(Number(value))}+ disabled={isLoadingSelectedTable}><Listbox.Option key="empty" className="min-w-96" value={1} label="---">---@@ -284,42 +297,87 @@ export const ForeignKeySelector = ({{fk.schema && fk.table && (<>- <div className="flex flex-col gap-y-3">- <label className="text-foreground-light text-sm">- Select columns from{' '}- <code className="text-xs">- {fk.schema}.{fk.table}- </code>{' '}- to reference to- </label>- <div className="grid grid-cols-10 gap-y-2">- <div className="col-span-5 text-xs text-foreground-lighter">- {selectedSchema}.{table.name.length > 0 ? table.name : '[unnamed table]'}- </div>- <div className="col-span-4 text-xs text-foreground-lighter text-right">- {fk.schema}.{fk.table}- </div>- {fk.columns.length === 0 && (- <Alert_Shadcn_ className="col-span-10 py-2 px-3">- <AlertDescription_Shadcn_>- There are no foreign key relations between the tables- </AlertDescription_Shadcn_>- </Alert_Shadcn_>- )}- {fk.columns.map((_, idx) => (- <Fragment key={`${uuidv4()}`}>- <div className="col-span-4">- <Listbox- id="column"- value={fk.columns[idx].source}- onChange={(value: string) => updateSelectedColumn(idx, 'source', value)}- >- <Listbox.Option key="empty" value={''} label="---" className="!w-[170px]">- ---- </Listbox.Option>- {(table?.columns ?? [])- .filter((x) => x.name.length !== 0)- .map((column) => (+ {isLoadingSelectedTable ? (+ <div className="flex py-6 flex-col items-center justify-center space-y-2">+ <Loader2 size={14} className="animate-spin" />+ <p className="text-sm text-foreground-light">Loading table columns</p>+ </div>+ ) : (+ <div className="flex flex-col gap-y-3">+ <label className="text-foreground-light text-sm">+ Select columns from{' '}+ <code className="text-xs">+ {fk.schema}.{fk.table}+ </code>{' '}+ to reference to+ </label>+ <div className="grid grid-cols-10 gap-y-2">+ <div className="col-span-5 text-xs text-foreground-lighter">+ {selectedSchema}.{table.name.length > 0 ? table.name : '[unnamed table]'}+ </div>+ <div className="col-span-4 text-xs text-foreground-lighter text-right">+ {fk.schema}.{fk.table}+ </div>+ {fk.columns.length === 0 && (+ <Alert_Shadcn_ className="col-span-10 py-2 px-3">+ <AlertDescription_Shadcn_>+ There are no foreign key relations between the tables+ </AlertDescription_Shadcn_>+ </Alert_Shadcn_>+ )}+ {fk.columns.map((_, idx) => (+ <Fragment key={`${fk.schema}-${fk.table}-${idx}`}>+ <div className="col-span-4">+ <Listbox+ id="column"+ value={fk.columns[idx].source}+ onChange={(value: string) => updateSelectedColumn(idx, 'source', value)}+ >+ <Listbox.Option+ key="empty"+ value={''}+ label="---"+ className="!w-[170px]"+ >+ ---+ </Listbox.Option>+ {(table?.columns ?? [])+ .filter((x) => x.name.length !== 0)+ .map((column) => (+ <Listbox.Option+ key={column.id}+ value={column.name}+ label={column.name}+ className="!w-[170px]"+ >+ <div className="flex items-center gap-2">+ <span className="text-foreground">{column.name}</span>+ <span className="text-foreground-lighter">+ {column.format === '' ? '-' : column.format}+ </span>+ </div>+ </Listbox.Option>+ ))}+ </Listbox>+ </div>+ <div className="col-span-1 flex justify-center items-center">+ <ArrowRight />+ </div>+ <div className="col-span-4">+ <Listbox+ id="column"+ value={fk.columns[idx].target}+ onChange={(value: string) => updateSelectedColumn(idx, 'target', value)}+ >+ <Listbox.Option+ key="empty"+ value={''}+ label="---"+ className="!w-[170px]"+ >+ ---+ </Listbox.Option>+ {(selectedTable?.columns ?? []).map((column) => (<Listbox.Optionkey={column.id}value={column.name}@@ -328,194 +386,171 @@ export const ForeignKeySelector = ({><div className="flex items-center gap-2"><span className="text-foreground">{column.name}</span>- <span className="text-foreground-lighter">- {column.format === '' ? '-' : column.format}- </span>+ <span className="text-foreground-lighter">{column.format}</span></div></Listbox.Option>))}- </Listbox>- </div>- <div className="col-span-1 flex justify-center items-center">- <ArrowRight />- </div>- <div className="col-span-4">- <Listbox- id="column"- value={fk.columns[idx].target}- onChange={(value: string) => updateSelectedColumn(idx, 'target', value)}- >- <Listbox.Option key="empty" value={''} label="---" className="!w-[170px]">- ---- </Listbox.Option>- {(selectedTable?.columns ?? []).map((column) => (- <Listbox.Option- key={column.id}- value={column.name}- label={column.name}- className="!w-[170px]"- >- <div className="flex items-center gap-2">- <span className="text-foreground">{column.name}</span>- <span className="text-foreground-lighter">{column.format}</span>- </div>- </Listbox.Option>- ))}- </Listbox>- </div>- <div className="col-span-1 flex justify-end items-center">- <Button- type="default"- className="px-1"- icon={<X />}- disabled={fk.columns.length === 1}- onClick={() => onRemoveColumn(idx)}- />- </div>- </Fragment>- ))}- </div>- <div className="space-y-2">- <Button type="default" onClick={addColumn}>- Add another column- </Button>- {errors.columns && <p className="text-red-900 text-sm">{errors.columns}</p>}- {hasTypeErrors && (- <Alert_Shadcn_ variant="warning">- <AlertTitle_Shadcn_>Column types do not match</AlertTitle_Shadcn_>- <AlertDescription_Shadcn_>- The following columns cannot be referenced as they are not of the same type:- </AlertDescription_Shadcn_>- <ul className="list-disc pl-5 mt-2 text-foreground-light">- {(errors?.types ?? []).map((x, idx: number) => {- if (x === undefined) return null- return (- <li key={`type-error-${idx}`}>- <code className="text-xs">{fk.columns[idx]?.source}</code> (- {x.sourceType}) and{' '}- <code className="text-xs">{fk.columns[idx]?.target}</code>(- {x.targetType})- </li>- )- })}- </ul>- </Alert_Shadcn_>- )}- {hasTypeNotices && (- <Alert_Shadcn_>- <AlertTitle_Shadcn_>Column types will be updated</AlertTitle_Shadcn_>- <AlertDescription_Shadcn_>- The following columns will have their types updated to match their- referenced column- </AlertDescription_Shadcn_>- <ul className="list-disc pl-5 mt-2 text-foreground-light">- {(errors?.typeNotice ?? []).map((x, idx: number) => {- if (x === undefined) return null- return (- <li key={`type-error-${idx}`}>- <div className="flex items-center gap-x-1">- <code className="text-xs">{fk.columns[idx]?.source}</code>{' '}- <ArrowRight size={14} /> {x.targetType}- </div>- </li>- )- })}- </ul>- </Alert_Shadcn_>- )}- </div>- </div>-- <SidePanel.Separator />-- <InformationBox- icon={<HelpCircle size="20" strokeWidth={1.5} />}- title="Which action is most appropriate?"- description={- <>- <p>- The choice of the action depends on what kinds of objects the related tables- represent:- </p>- <ul className="mt-2 list-disc pl-4 space-y-1">- <li>- <code className="text-xs">Cascade</code>: if the referencing table- represents something that is a component of what is represented by the- referenced table and cannot exist independently- </li>- <li>- <code className="text-xs">Restrict</code> or{' '}- <code className="text-xs">No action</code>: if the two tables represent- independent objects- </li>- <li>- <code className="text-xs">Set NULL</code> or{' '}- <code className="text-xs">Set default</code>: if a foreign-key relationship- represents optional information- </li>- </ul>- <p className="mt-2">- Typically, restricting and cascading deletes are the most common options, but- the default behavior is no action- </p>- </>- }- url="https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-FK"- urlLabel="More information"- />-- <Listbox- id="updateAction"- value={fk.updateAction}- label="Action if referenced row is updated"- descriptionText={- <p>- {generateCascadeActionDescription(- 'update',- fk.updateAction,- `${fk.schema}.${fk.table}`+ </Listbox>+ </div>+ <div className="col-span-1 flex justify-end items-center">+ <Button+ type="default"+ className="px-1"+ icon={<X />}+ disabled={fk.columns.length === 1}+ onClick={() => onRemoveColumn(idx)}+ />+ </div>+ </Fragment>+ ))}+ </div>+ <div className="space-y-2">+ <Button type="default" onClick={addColumn}>+ Add another column+ </Button>+ {errors.columns && <p className="text-red-900 text-sm">{errors.columns}</p>}+ {hasTypeErrors && (+ <Alert_Shadcn_ variant="warning">+ <AlertTitle_Shadcn_>Column types do not match</AlertTitle_Shadcn_>+ <AlertDescription_Shadcn_>+ The following columns cannot be referenced as they are not of the same+ type:+ </AlertDescription_Shadcn_>+ <ul className="list-disc pl-5 mt-2 text-foreground-light">+ {(errors?.types ?? []).map((x, idx: number) => {+ if (x === undefined) return null+ return (+ <li key={`type-error-${idx}`}>+ <code className="text-xs">{fk.columns[idx]?.source}</code> (+ {x.sourceType}) and{' '}+ <code className="text-xs">{fk.columns[idx]?.target}</code>(+ {x.targetType})+ </li>+ )+ })}+ </ul>+ </Alert_Shadcn_>+ )}+ {hasTypeNotices && (+ <Alert_Shadcn_>+ <AlertTitle_Shadcn_>Column types will be updated</AlertTitle_Shadcn_>+ <AlertDescription_Shadcn_>+ The following columns will have their types updated to match their+ referenced column+ </AlertDescription_Shadcn_>+ <ul className="list-disc pl-5 mt-2 text-foreground-light">+ {(errors?.typeNotice ?? []).map((x, idx: number) => {+ if (x === undefined) return null+ return (+ <li key={`type-error-${idx}`}>+ <div className="flex items-center gap-x-1">+ <code className="text-xs">{fk.columns[idx]?.source}</code>{' '}+ <ArrowRight size={14} /> {x.targetType}+ </div>+ </li>+ )+ })}+ </ul>+ </Alert_Shadcn_>)}- </p>- }- onChange={(value: string) => updateCascadeAction('updateAction', value)}- >- {FOREIGN_KEY_CASCADE_OPTIONS.filter((option) =>- ['no-action', 'cascade', 'restrict'].includes(option.key)- ).map((option) => (- <Listbox.Option key={option.key} value={option.value} label={option.label}>- <p className="text-foreground">{option.label}</p>- </Listbox.Option>- ))}- </Listbox>-- <Listbox- id="deletionAction"- value={fk.deletionAction}- className="[&>div>label]:flex [&>div>label]:items-center"- label="Action if referenced row is removed"- // @ts-ignore- labelOptional={- <DocsButton href={`${DOCS_URL}/guides/database/postgres/cascade-deletes`} />- }- descriptionText={- <>- <p>- {generateCascadeActionDescription(- 'delete',- fk.deletionAction,- `${fk.schema}.${fk.table}`- )}- </p>- </>- }- onChange={(value: string) => updateCascadeAction('deletionAction', value)}- >- {FOREIGN_KEY_CASCADE_OPTIONS.map((option) => (- <Listbox.Option key={option.key} value={option.value} label={option.label}>- <p className="text-foreground">{option.label}</p>- </Listbox.Option>- ))}- </Listbox>+ </div>+ </div>+ )}++ {!isLoadingSelectedTable && (+ <>+ <SidePanel.Separator />++ <InformationBox+ icon={<HelpCircle size="20" strokeWidth={1.5} />}+ title="Which action is most appropriate?"+ description={+ <>+ <p>+ The choice of the action depends on what kinds of objects the related+ tables represent:+ </p>+ <ul className="mt-2 list-disc pl-4 space-y-1">+ <li>+ <code className="text-xs">Cascade</code>: if the referencing table+ represents something that is a component of what is represented by the+ referenced table and cannot exist independently+ </li>+ <li>+ <code className="text-xs">Restrict</code> or{' '}+ <code className="text-xs">No action</code>: if the two tables represent+ independent objects+ </li>+ <li>+ <code className="text-xs">Set NULL</code> or{' '}+ <code className="text-xs">Set default</code>: if a foreign-key+ relationship represents optional information+ </li>+ </ul>+ <p className="mt-2">+ Typically, restricting and cascading deletes are the most common options,+ but the default behavior is no action+ </p>+ </>+ }+ url="https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-FK"+ urlLabel="More information"+ />++ <Listbox+ id="updateAction"+ value={fk.updateAction}+ label="Action if referenced row is updated"+ descriptionText={+ <p>+ {generateCascadeActionDescription(+ 'update',+ fk.updateAction,+ `${fk.schema}.${fk.table}`+ )}+ </p>+ }+ onChange={(value: string) => updateCascadeAction('updateAction', value)}+ >+ {FOREIGN_KEY_CASCADE_OPTIONS.filter((option) =>+ ['no-action', 'cascade', 'restrict'].includes(option.key)+ ).map((option) => (+ <Listbox.Option key={option.key} value={option.value} label={option.label}>+ <p className="text-foreground">{option.label}</p>+ </Listbox.Option>+ ))}+ </Listbox>++ <Listbox+ id="deletionAction"+ value={fk.deletionAction}+ className="[&>div>label]:flex [&>div>label]:items-center"+ label="Action if referenced row is removed"+ // @ts-ignore+ labelOptional={+ <DocsButton href={`${DOCS_URL}/guides/database/postgres/cascade-deletes`} />+ }+ descriptionText={+ <>+ <p>+ {generateCascadeActionDescription(+ 'delete',+ fk.deletionAction,+ `${fk.schema}.${fk.table}`+ )}+ </p>+ </>+ }+ onChange={(value: string) => updateCascadeAction('deletionAction', value)}+ >+ {FOREIGN_KEY_CASCADE_OPTIONS.map((option) => (+ <Listbox.Option key={option.key} value={option.value} label={option.label}>+ <p className="text-foreground">{option.label}</p>+ </Listbox.Option>+ ))}+ </Listbox>+ </>+ )}</>)}</div>diff --git a/e2e/studio/features/table-editor.spec.ts b/e2e/studio/features/table-editor.spec.tsindex 35781606356..0165c3e824f 100644--- a/e2e/studio/features/table-editor.spec.ts+++ b/e2e/studio/features/table-editor.spec.ts@@ -231,7 +231,7 @@ test.describe.serial('table editor', () => {page,'pg-meta',ref,- 'tables?include_columns=true&included_schemas=public'+ 'tables?include_columns=false&included_schemas=public') // wait for table creationawait page.getByRole('button', { name: `View ${tableNameRlsDisabled}` }).click()await expect(page.getByRole('button', { name: 'RLS disabled' })).toBeVisible()