Skip to content

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

Invalidate foreign key constraints query after saving column

Joshen Lim10a10a5d7c65
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
index 6ea51394495..3917cca19bc 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
@@ -75,7 +75,7 @@ const ColumnEditor: FC<Props> = ({
const [columnFields, setColumnFields] = useState<ColumnField>()
const [isEditingRelation, setIsEditingRelation] = useState<boolean>(false)
- const { data, isLoading } = useForeignKeyConstraintsQuery({
+ const { data } = useForeignKeyConstraintsQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
schema: selectedTable?.schema,
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
index a4791589a4f..4a74c8d051e 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
@@ -221,7 +221,7 @@ export const getColumnForeignKey = (
})
if (foreignKey === undefined) return foreignKey
else {
- const foreignKeyMeta = foreignKeys.find((fk) => fk.id === foreignKey?.id)
+ const foreignKeyMeta = foreignKeys.find((fk) => fk.id === foreignKey.id)
return { ...foreignKey, deletion_action: foreignKeyMeta?.deletion_action ?? 'a' }
}
}
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx
index 93289f96640..b9dacdbd220 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx
@@ -245,11 +245,12 @@ const ColumnForeignKeyUpdated: FC<{
<code className="text-xs font-mono">{updatedKey}</code>
</div>
<div className="flex flex-col space-y-2">
- {originalDeletionAction !== updatedDeletionAction && (
- <Badge color="gray" className="line-through">
- Delete: {originalDeletionAction}
- </Badge>
- )}
+ {originalDeletionAction !== undefined &&
+ originalDeletionAction !== updatedDeletionAction && (
+ <Badge color="gray" className="line-through">
+ Delete: {originalDeletionAction}
+ </Badge>
+ )}
{updatedDeletionAction !== undefined && (
<Badge color="green">Delete: {updatedDeletionAction}</Badge>
)}
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
index 998954457a3..99cd71e1ff0 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.tsx
@@ -19,6 +19,8 @@ import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectConte
import ConfirmationModal from 'components/ui/ConfirmationModal'
import JsonEdit from './RowEditor/JsonEditor/JsonEditor'
import { JsonEditValue } from './RowEditor/RowEditor.types'
+import { useQueryClient } from '@tanstack/react-query'
+import { sqlKeys } from 'data/sql/keys'
interface Props {
selectedSchema: string
@@ -55,6 +57,7 @@ const SidePanelEditor: FC<Props> = ({
onColumnSaved = () => {},
}) => {
const { meta, ui } = useStore()
+ const queryClient = useQueryClient()
const [isEdited, setIsEdited] = useState<boolean>(false)
const [isClosingPanel, setIsClosingPanel] = useState<boolean>(false)
@@ -178,7 +181,12 @@ const SidePanelEditor: FC<Props> = ({
if (response?.error) {
ui.setNotification({ category: 'error', message: response.error.message })
} else {
+ ui.setNotification({
+ category: 'success',
+ message: isNewRecord ? `Successfully created column` : `Successfully updated column`,
+ })
await meta.tables.loadById(selectedTable!.id)
+ queryClient.invalidateQueries(sqlKeys.query(project?.ref, ['foreignKeyConstraints']))
onColumnSaved(configuration.isEncrypted)
setIsEdited(false)
closePanel()
@@ -274,6 +282,7 @@ const SidePanelEditor: FC<Props> = ({
})
}
}
+ queryClient.invalidateQueries(sqlKeys.query(project?.ref, ['foreignKeyConstraints']))
} catch (error: any) {
saveTableError = true
ui.setNotification({ id: toastId, category: 'error', message: error.message })
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
index 0d20cee9e41..5cd081391fe 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
@@ -17,7 +17,8 @@ import {
generateTableFieldFromPostgresTable,
formatImportedContentToColumnFields,
} from './TableEditor.utils'
-import InformationBox from 'components/ui/InformationBox'
+import { useForeignKeyConstraintsQuery } from 'data/database/foreign-key-constraints-query'
+import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
interface Props {
table?: PostgresTable
@@ -51,6 +52,7 @@ const TableEditor: FC<Props> = ({
updateEditorDirty = () => {},
}) => {
const { ui, meta } = useStore()
+ const { project } = useProjectContext()
const isNewRecord = isUndefined(table)
const tables = meta.tables.list()
@@ -73,6 +75,13 @@ const TableEditor: FC<Props> = ({
const [importContent, setImportContent] = useState<ImportContent>()
const [isImportingSpreadsheet, setIsImportingSpreadsheet] = useState<boolean>(false)
+ const { data } = useForeignKeyConstraintsQuery({
+ projectRef: project?.ref,
+ connectionString: project?.connectionString,
+ schema: table?.schema,
+ })
+ const foreignKeyMeta = data?.result ?? []
+
useEffect(() => {
if (visible) {
setErrors({})
@@ -84,6 +93,7 @@ const TableEditor: FC<Props> = ({
} else {
const tableFields = generateTableFieldFromPostgresTable(
table!,
+ foreignKeyMeta,
isDuplicating,
isRealtimeEnabled
)
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.utils.ts
index 2f0d60f983a..e58420703c6 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.utils.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.utils.ts
@@ -8,6 +8,7 @@ import {
generateColumnField,
generateColumnFieldFromPostgresColumn,
} from '../ColumnEditor/ColumnEditor.utils'
+import { ForeignKeyConstraint } from 'data/database/foreign-key-constraints-query'
export const validateFields = (field: TableField) => {
const errors = {} as any
@@ -36,6 +37,7 @@ export const generateTableField = (): TableField => {
export const generateTableFieldFromPostgresTable = (
table: PostgresTable,
+ foreignKeys: ForeignKeyConstraint[],
isDuplicating = false,
isRealtimeEnabled = false
): TableField => {
@@ -45,7 +47,7 @@ export const generateTableFieldFromPostgresTable = (
comment: isDuplicating ? `This is a duplicate of ${table.name}` : table?.comment ?? '',
// @ts-ignore
columns: table.columns.map((column: PostgresColumn) => {
- return generateColumnFieldFromPostgresColumn(column, table)
+ return generateColumnFieldFromPostgresColumn(column, table, foreignKeys)
}),
isRLSEnabled: table.rls_enabled,
isRealtimeEnabled,