Skip to content

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

fix(ux): allow creating fk on new column after changing name (#23204)

Charis7d15462d5d4d
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
index 5f3c5eca82b..fb6d8cabf7f 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
@@ -9,8 +9,6 @@ import {
Alert_Shadcn_,
Button,
Checkbox,
- IconExternalLink,
- IconPlus,
Input,
SidePanel,
Toggle,
@@ -29,6 +27,7 @@ import {
} from 'data/database/foreign-key-constraints-query'
import { useEnumeratedTypesQuery } from 'data/enumerated-types/enumerated-types-query'
import { EXCLUDED_SCHEMAS_WITHOUT_EXTENSIONS } from 'lib/constants/schemas'
+import { ExternalLink, Plus } from 'lucide-react'
import type { Dictionary } from 'types'
import { WarningIcon } from 'ui-patterns/Icons/StatusIcons'
import ActionBar from '../ActionBar'
@@ -140,6 +139,22 @@ const ColumnEditor = ({
changes.defaultValue = null
}
+ const changedName = 'name' in changes && changes.name !== columnFields.name
+
+ if (
+ changedName &&
+ fkRelations.find((fk) => fk.columns.find(({ source }) => source === columnFields?.name))
+ ) {
+ setFkRelations(
+ fkRelations.map((relation) => ({
+ ...relation,
+ columns: relation.columns.map((col) =>
+ col.source === columnFields?.name ? { ...col, source: changes.name! } : col
+ ),
+ }))
+ )
+ }
+
const updatedColumnFields = { ...columnFields, ...changes } as ColumnField
setColumnFields(updatedColumnFields)
updateEditorDirty()
@@ -223,7 +238,7 @@ const ColumnEditor = ({
asChild
type="default"
size="tiny"
- icon={<IconPlus size={14} strokeWidth={2} />}
+ icon={<Plus size={14} strokeWidth={2} />}
>
<Link href={`/project/${ref}/database/types`} target="_blank" rel="noreferrer">
Create enum types
@@ -233,7 +248,7 @@ const ColumnEditor = ({
asChild
type="default"
size="tiny"
- icon={<IconExternalLink size={14} strokeWidth={2} />}
+ icon={<ExternalLink size={14} strokeWidth={2} />}
>
<Link
href="https://supabase.com/docs/guides/database/tables#data-types"
@@ -374,7 +389,7 @@ const ColumnEditor = ({
</Link>{' '}
Transparent Column Encryption (TCE).
</p>
- <Button asChild type="default" icon={<IconExternalLink />} className="mt-2">
+ <Button asChild type="default" icon={<ExternalLink />} className="mt-2">
<Link
target="_blank"
rel="noreferrer"
@@ -389,16 +404,16 @@ const ColumnEditor = ({
</FormSection>
<SidePanel.Separator />
- {/* TODO: need to pull column privileges in here
- if any columns are using column-level privileges, show this warning */}
- <FormSection
+ {/* TODO: need to pull column privileges in here if any columns are using column-level privileges, show this warning */}
+ {/* [Joshen] This shouldn't show up for all tables */}
+ {/* <FormSection
header={
<FormSectionLabel className="lg:!col-span-4">Column privileges</FormSectionLabel>
}
>
<FormSectionContent loading={false} className="lg:!col-span-8">
<Alert_Shadcn_ variant="warning">
- <WarningIcon strokeWidth={2} />
+ <WarningIcon />
<AlertTitle_Shadcn_>This table uses column-privileges</AlertTitle_Shadcn_>
<AlertDescription_Shadcn_>
<p>
@@ -415,7 +430,7 @@ const ColumnEditor = ({
</AlertDescription_Shadcn_>
</Alert_Shadcn_>
</FormSectionContent>
- </FormSection>
+ </FormSection> */}
</>
)}
</SidePanel>
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx
index 7b398f584de..0efd4f816d2 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx
@@ -111,7 +111,10 @@ const ColumnForeignKey = ({
columns:
column.isNewColumn && column.name
? (table.columns as any[]).concat(column)
- : (table.columns as any[]),
+ : (table.columns as any[]).map((c) => {
+ if (c.id === column.id) return { ...c, name: column.name }
+ else return c
+ }),
}}
foreignKey={selectedFk}
onClose={() => {
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/ForeignKeysManagement/ForeignKeyRow.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/ForeignKeysManagement/ForeignKeyRow.tsx
index 33f41e76e87..82d0142d26a 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/ForeignKeysManagement/ForeignKeyRow.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/ForeignKeysManagement/ForeignKeyRow.tsx
@@ -2,7 +2,8 @@ import clsx from 'clsx'
import { useParams } from 'common'
import Link from 'next/link'
import SVG from 'react-inlinesvg'
-import { Badge, Button, IconArrowRight } from 'ui'
+import { Badge, Button, cn } from 'ui'
+import { ArrowRight } from 'lucide-react'
import { BASE_PATH } from 'lib/constants'
import type { ForeignKey } from '../../ForeignKeySelector/ForeignKeySelector.types'
@@ -89,8 +90,12 @@ export const ForeignKeyRow = ({
<div className="flex flex-col gap-y-1">
{foreignKey.columns.map((x, idx) => (
<div key={`relation-${idx}}`} className="flex items-center gap-x-2">
- <code className="text-xs">{x.source}</code>
- <IconArrowRight />
+ <code
+ className={cn('text-xs', (x?.source ?? '').length === 0 && 'text-foreground-light')}
+ >
+ {x.source || '[column_name]'}
+ </code>
+ <ArrowRight size={16} />
<code className="text-xs">
{foreignKey.schema}.{foreignKey.table}.{x.target}
</code>