Skip to content

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

Add set null cta in ForeignRowSelector (#32762)

Joshen Lima143d8905088
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
index 1840497c8e4..51942156df5 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
@@ -3,6 +3,7 @@ import { useState } from 'react'
import { DndProvider } from 'react-dnd'
import { HTML5Backend } from 'react-dnd-html5-backend'
+import { useParams } from 'common'
import {
formatFilterURLParams,
formatSortURLParams,
@@ -15,7 +16,7 @@ import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectConte
import { useTableEditorQuery } from 'data/table-editor/table-editor-query'
import { useTableRowsQuery } from 'data/table-rows/table-rows-query'
import { useRoleImpersonationStateSnapshot } from 'state/role-impersonation-state'
-import { SidePanel } from 'ui'
+import { Button, SidePanel } from 'ui'
import ActionBar from '../../ActionBar'
import { ForeignKey } from '../../ForeignKeySelector/ForeignKeySelector.types'
import Pagination from './Pagination'
@@ -34,11 +35,22 @@ const ForeignRowSelector = ({
onSelect,
closePanel,
}: ForeignRowSelectorProps) => {
+ const { id } = useParams()
const { project } = useProjectContext()
+ const { data: selectedTable } = useTableEditorQuery({
+ projectRef: project?.ref,
+ connectionString: project?.connectionString,
+ id: !!id ? Number(id) : undefined,
+ })
const { tableId: _tableId, schema: schemaName, table: tableName, columns } = foreignKey ?? {}
const tableId = _tableId ? Number(_tableId) : undefined
+ // [Joshen] Only show Set NULL CTA if its a 1:1 foreign key, and source column is nullable
+ // As this wouldn't be straightforward for composite foreign keys
+ const sourceColumn = (selectedTable?.columns ?? []).find((c) => c.name === columns?.[0].source)
+ const isNullable = (columns ?? []).length === 1 && sourceColumn?.is_nullable
+
const { data: table } = useTableEditorQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
@@ -135,13 +147,27 @@ const ForeignRowSelector = ({
</DndProvider>
</div>
- <Pagination
- page={page}
- setPage={setPage}
- rowsPerPage={rowsPerPage}
- currentPageRowsCount={data?.rows.length ?? 0}
- isLoading={isRefetching}
- />
+ <div className="flex items-center gap-x-3 divide-x">
+ <Pagination
+ page={page}
+ setPage={setPage}
+ rowsPerPage={rowsPerPage}
+ currentPageRowsCount={data?.rows.length ?? 0}
+ isLoading={isRefetching}
+ />
+ {isNullable && (
+ <div className="pl-3">
+ <Button
+ type="default"
+ onClick={() => {
+ if (columns?.length === 1) onSelect({ [columns[0].source]: null })
+ }}
+ >
+ Set NULL
+ </Button>
+ </div>
+ )}
+ </div>
</div>
{data.rows.length > 0 ? (
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
index 54024583553..71135739733 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
@@ -1,5 +1,5 @@
import { includes, noop } from 'lodash'
-import { Edit, Edit2, Link } from 'lucide-react'
+import { Edit, Edit2 } from 'lucide-react'
import {
Button,
@@ -120,15 +120,19 @@ const InputField = ({
error={errors[field.name]}
onChange={(event: any) => onUpdateField({ [field.name]: event.target.value })}
actions={
- <Button
- type="default"
- className="mr-1"
- htmlType="button"
- onClick={onSelectForeignKey}
- icon={<Link />}
- >
- Select record
- </Button>
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button type="default" icon={<Edit />} className="px-1.5" />
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end" className="w-28">
+ {field.isNullable && (
+ <DropdownMenuItem onClick={() => onUpdateField({ [field.name]: null })}>
+ Set to NULL
+ </DropdownMenuItem>
+ )}
+ <DropdownMenuItem onClick={onSelectForeignKey}>Select record</DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
}
/>
)