Skip to content

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

Contextual error toasts for deleting referenced rows (#23135)

Joshen Lim0d292c668d5c
diff --git a/apps/studio/components/interfaces/TableGridEditor/DeleteConfirmationDialogs.tsx b/apps/studio/components/interfaces/TableGridEditor/DeleteConfirmationDialogs.tsx
index a76fd8a4bf6..46dbd43eb41 100644
--- a/apps/studio/components/interfaces/TableGridEditor/DeleteConfirmationDialogs.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/DeleteConfirmationDialogs.tsx
@@ -103,9 +103,6 @@ const DeleteConfirmationDialogs = ({
}
toast.success(`Successfully deleted selected row(s)`)
},
- onError: (error) => {
- toast.error(`Failed to delete row: ${error.message}`)
- },
onSettled: () => {
snap.closeConfirmationDialog()
},
diff --git a/apps/studio/data/table-rows/table-row-delete-mutation.ts b/apps/studio/data/table-rows/table-row-delete-mutation.tsx
similarity index 50%
rename from apps/studio/data/table-rows/table-row-delete-mutation.ts
rename to apps/studio/data/table-rows/table-row-delete-mutation.tsx
index 9ebd685c439..1957b5a3330 100644
--- a/apps/studio/data/table-rows/table-row-delete-mutation.ts
+++ b/apps/studio/data/table-rows/table-row-delete-mutation.tsx
@@ -1,7 +1,10 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { ExternalLink } from 'lucide-react'
import { toast } from 'react-hot-toast'
+import { Button, toast as UiToast } from 'ui'
import { Query, SupaRow } from 'components/grid'
+import { Markdown } from 'components/interfaces/Markdown'
import { executeSql } from 'data/sql/execute-sql-query'
import { sqlKeys } from 'data/sql/keys'
import type { Table } from 'data/tables/table-query'
@@ -78,7 +81,52 @@ export const useTableRowDeleteMutation = ({
},
async onError(data, variables, context) {
if (onError === undefined) {
- toast.error(`Failed to delete table row: ${data.message}`)
+ const { table, rows } = variables
+ const isFkError = data.message.includes('violates foreign key constraint')
+ const isMultipleRows = rows.length > 1
+
+ if (isFkError) {
+ const sourceTable = table.name
+ const referencingTable = data.message.split('on table ')[2].replaceAll('"', '')
+ const fkName = data.message
+ .split('foreign key constraint')[1]
+ .split('on table')[0]
+ .replaceAll('"', '')
+ const initialMessage = isMultipleRows
+ ? `Unable to delete rows as one of them is currently referenced by a foreign key constraint from the table \`${referencingTable}\`.`
+ : `Unable to delete row as it is currently referenced by a foreign key constraint from the table \`${referencingTable}\`.`
+ const resolutionCTA = `Set an on delete behavior on the foreign key relation \`${fkName}\` in the \`${referencingTable}\` table to automatically respond when row(s) are being deleted in the \`${sourceTable}\` table.`
+
+ UiToast({
+ variant: 'default',
+ style: { flexDirection: 'column' },
+ title: (
+ <Markdown content={initialMessage} className="text-foreground [&>p]:m-0" />
+ ) as any,
+ description: <Markdown content={resolutionCTA} className="[&>p]:m-0" />,
+ action: (
+ <div className="w-full flex gap-x-2 !mx-0 mt-3">
+ {/* [Joshen] Ideally we also are able to add this CTA but we can't guarantee this info without an on-demand fetch */}
+ {/* <Button asChild key="cta-1" type="default">
+ <Link href={`/project/${projectRef}/editor`}>
+ View "{referencingTable}" table
+ </Link>
+ </Button> */}
+ <Button asChild type="outline" icon={<ExternalLink />}>
+ <a
+ target="_blank"
+ rel="noreferrer"
+ href="https://supabase.com/docs/guides/database/postgres/cascade-deletes"
+ >
+ Documentation
+ </a>
+ </Button>
+ </div>
+ ),
+ })
+ } else {
+ toast.error(`Failed to delete table row: ${data.message}`)
+ }
} else {
onError(data, variables, context)
}
diff --git a/apps/studio/data/tables/table-query.ts b/apps/studio/data/tables/table-query.ts
index ae9b159e9f3..2e8bea2b5dd 100644
--- a/apps/studio/data/tables/table-query.ts
+++ b/apps/studio/data/tables/table-query.ts
@@ -1,10 +1,9 @@
import type { PostgresTable } from '@supabase/postgres-meta'
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
-// import { get } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
-import { tableKeys } from './keys'
import { useCallback } from 'react'
+
import { get, handleError } from 'data/fetchers'
+import { tableKeys } from './keys'
export type TableVariables = {
id?: number
diff --git a/packages/ui/src/layout/PortalToast/PortalToast.tsx b/packages/ui/src/layout/PortalToast/PortalToast.tsx
index 38b646d3619..69d808ff15e 100644
--- a/packages/ui/src/layout/PortalToast/PortalToast.tsx
+++ b/packages/ui/src/layout/PortalToast/PortalToast.tsx
@@ -42,7 +42,7 @@ const PortalToast = () => (
? 'max-w-[380px]'
: isConsentToast
? 'max-w-none sm:max-w-[800px]'
- : 'max-w-[260px]'
+ : 'max-w-[280px]'
}`}
>
{message}