Skip to content

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

Implement cascade update support

Joshen Lim51e72153ec72
diff --git a/studio/components/grid/components/grid/ColumnHeader.tsx b/studio/components/grid/components/grid/ColumnHeader.tsx
index 4ba3545b2c0..5c490e82791 100644
--- a/studio/components/grid/components/grid/ColumnHeader.tsx
+++ b/studio/components/grid/components/grid/ColumnHeader.tsx
@@ -8,7 +8,7 @@ import { ColumnHeaderProps, ColumnType, DragItem, GridForeignKey } from '../../t
import { ColumnMenu } from '../menu'
import { useTrackedState } from '../../store'
import { FOREIGN_KEY_CASCADE_ACTION } from 'data/database/database-query-constants'
-import { getForeignKeyDeletionAction } from 'components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils'
+import { getForeignKeyCascadeAction } from 'components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils'
export function ColumnHeader<R>({
column,
@@ -209,7 +209,7 @@ function renderColumnIcon(
</div>
{foreignKey?.deletionAction !== FOREIGN_KEY_CASCADE_ACTION.NO_ACTION && (
<p className="text-xs text-scale-1200 mt-1">
- On delete: {getForeignKeyDeletionAction(foreignKey?.deletionAction)}
+ On delete: {getForeignKeyCascadeAction(foreignKey?.deletionAction)}
</p>
)}
</div>
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
index fa73d7cbd6b..3e049682945 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.tsx
@@ -12,7 +12,9 @@ import { useEffect, useState } from 'react'
import { Button, Checkbox, IconExternalLink, Input, SidePanel, Toggle } from 'ui'
import { EncryptionKeySelector } from 'components/interfaces/Settings/Vault'
+import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { FormSection, FormSectionContent, FormSectionLabel } from 'components/ui/Forms'
+import { useForeignKeyConstraintsQuery } from 'data/database/foreign-key-constraints-query'
import { useStore } from 'hooks'
import { ForeignKeySelector } from '..'
import ActionBar from '../ActionBar'
@@ -36,9 +38,6 @@ import ColumnForeignKey from './ColumnForeignKey'
import ColumnType from './ColumnType'
import HeaderTitle from './HeaderTitle'
-import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
-import { useForeignKeyConstraintsQuery } from 'data/database/foreign-key-constraints-query'
-
export interface ColumnEditorProps {
column?: PostgresColumn
selectedTable: PostgresTable
@@ -130,6 +129,7 @@ const ColumnEditor = ({
table: PostgresTable
column: PostgresColumn
deletionAction: string
+ updateAction: string
}) => {
onUpdateField({
foreignKey:
@@ -144,6 +144,7 @@ const ColumnEditor = ({
target_table_name: foreignKeyConfiguration.table.name,
target_column_name: foreignKeyConfiguration.column.name,
deletion_action: foreignKeyConfiguration.deletionAction,
+ update_action: foreignKeyConfiguration.updateAction,
}
: undefined,
...(foreignKeyConfiguration !== undefined && {
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
index aef2c4b48b3..2ab781a488b 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnEditor.utils.ts
@@ -193,19 +193,20 @@ export const getForeignKeyUIState = (
originalConfig: ExtendedPostgresRelationship | undefined,
updatedConfig: ExtendedPostgresRelationship | undefined
): 'Info' | 'Add' | 'Remove' | 'Update' => {
- if (isUndefined(originalConfig) && !isUndefined(updatedConfig)) {
+ if (originalConfig === undefined && updatedConfig !== undefined) {
return 'Add'
}
- if (!isUndefined(originalConfig) && isUndefined(updatedConfig)) {
+ if (originalConfig !== undefined && updatedConfig === undefined) {
return 'Remove'
}
if (
- !isEqual(originalConfig?.target_table_schema, updatedConfig?.target_table_schema) ||
- !isEqual(originalConfig?.target_table_name, updatedConfig?.target_table_name) ||
- !isEqual(originalConfig?.target_column_name, updatedConfig?.target_column_name) ||
- originalConfig?.deletion_action !== updatedConfig?.deletion_action
+ originalConfig?.target_table_schema !== updatedConfig?.target_table_schema ||
+ originalConfig?.target_table_name !== updatedConfig?.target_table_name ||
+ originalConfig?.target_column_name !== updatedConfig?.target_column_name ||
+ originalConfig?.deletion_action !== updatedConfig?.deletion_action ||
+ originalConfig?.update_action !== updatedConfig?.update_action
) {
return 'Update'
}
@@ -232,6 +233,7 @@ export const getColumnForeignKey = (
return {
...foreignKey,
deletion_action: foreignKeyMeta?.deletion_action ?? FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
+ update_action: foreignKeyMeta?.update_action ?? FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
}
}
}
@@ -242,8 +244,8 @@ const formatArrayToPostgresArray = (arrayString: string) => {
return arrayString.replaceAll('[', '{').replaceAll(']', '}')
}
-export const getForeignKeyDeletionAction = (deletionAction?: string) => {
- switch (deletionAction) {
+export const getForeignKeyCascadeAction = (action?: string) => {
+ switch (action) {
case FOREIGN_KEY_CASCADE_ACTION.CASCADE:
return 'Cascade'
case FOREIGN_KEY_CASCADE_ACTION.RESTRICT:
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx
index b8af6e7f008..b2cd2f4778c 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx
@@ -4,7 +4,7 @@ import { Badge, Button, IconArrowRight } from 'ui'
import InformationBox from 'components/ui/InformationBox'
import type { ExtendedPostgresRelationship } from '../SidePanelEditor.types'
import { ColumnField } from '../SidePanelEditor.types'
-import { getForeignKeyDeletionAction, getForeignKeyUIState } from './ColumnEditor.utils'
+import { getForeignKeyCascadeAction, getForeignKeyUIState } from './ColumnEditor.utils'
interface ColumnForeignKeyProps {
column: ColumnField
@@ -92,7 +92,9 @@ const ColumnForeignKeyInformation = ({
onSelectEditRelation,
onSelectRemoveRelation,
}: ColumnForeignKeyInformationProps) => {
- const deletionAction = getForeignKeyDeletionAction(foreignKey?.deletion_action)
+ const updateAction = getForeignKeyCascadeAction(foreignKey?.update_action)
+ const deletionAction = getForeignKeyCascadeAction(foreignKey?.deletion_action)
+
return (
<InformationBox
block
@@ -101,12 +103,15 @@ const ColumnForeignKeyInformation = ({
<div className="space-y-2">
<p className="text-scale-1100">This column has the following foreign key relation:</p>
<div className="flex items-center space-x-2 text-scale-1200">
- <span className="text-xs text-code font-mono">{columnName}</span>
+ <p className="text-xs text-code font-mono">{columnName}</p>
<IconArrowRight size={14} strokeWidth={2} />
- <span className="text-xs text-code font-mono">
+ <p className="text-xs text-code font-mono">
{foreignKey?.target_table_schema}.{foreignKey?.target_table_name}.
{foreignKey?.target_column_name}
- </span>
+ </p>
+ </div>
+ <div className="flex items-center space-x-2">
+ {updateAction !== undefined && <Badge color="gray">On update: {updateAction}</Badge>}
{deletionAction !== undefined && (
<Badge color="gray">On delete: {deletionAction}</Badge>
)}
@@ -139,7 +144,9 @@ const ColumnForeignKeyAdded = ({
onSelectEditRelation,
onSelectRemoveRelation,
}: ColumnForeignKeyAddedProps) => {
- const deletionAction = getForeignKeyDeletionAction(foreignKey?.deletion_action)
+ const updateAction = getForeignKeyCascadeAction(foreignKey?.update_action)
+ const deletionAction = getForeignKeyCascadeAction(foreignKey?.deletion_action)
+
return (
<InformationBox
block
@@ -150,18 +157,21 @@ const ColumnForeignKeyAdded = ({
The following foreign key relation will be <span className="text-brand">added</span>:
</span>
<div className="flex items-center space-x-2 text-scale-1200">
- <span
+ <p
className={`${
columnName.length > 0 ? 'text-code font-mono text-xs' : ''
} max-w-xs truncate`}
>
{columnName || 'This column'}
- </span>
+ </p>
<IconArrowRight size={14} strokeWidth={2} />
- <span className="max-w-xs text-xs truncate text-code font-mono">
+ <p className="max-w-xs text-xs truncate text-code font-mono">
{foreignKey?.target_table_schema}.{foreignKey?.target_table_name}.
{foreignKey?.target_column_name}
- </span>
+ </p>
+ </div>
+ <div className="flex items-center space-x-2">
+ {updateAction !== undefined && <Badge color="gray">On update: {updateAction}</Badge>}
{deletionAction !== undefined && (
<Badge color="gray">On delete: {deletionAction}</Badge>
)}
@@ -245,8 +255,11 @@ const ColumnForeignKeyUpdated = ({
const originalKey = `${originalForeignKey?.target_table_schema}.${originalForeignKey?.target_table_name}.${originalForeignKey?.target_column_name}`
const updatedKey = `${updatedForeignKey?.target_table_schema}.${updatedForeignKey?.target_table_name}.${updatedForeignKey?.target_column_name}`
- const originalDeletionAction = getForeignKeyDeletionAction(originalForeignKey?.deletion_action)
- const updatedDeletionAction = getForeignKeyDeletionAction(updatedForeignKey?.deletion_action)
+ const originalUpdateAction = getForeignKeyCascadeAction(originalForeignKey?.update_action)
+ const updatedUpdateAction = getForeignKeyCascadeAction(updatedForeignKey?.update_action)
+
+ const originalDeletionAction = getForeignKeyCascadeAction(originalForeignKey?.deletion_action)
+ const updatedDeletionAction = getForeignKeyCascadeAction(updatedForeignKey?.deletion_action)
return (
<InformationBox
@@ -268,19 +281,26 @@ const ColumnForeignKeyUpdated = ({
)}
<code className="text-xs font-mono">{updatedKey}</code>
</div>
- <div className="flex flex-col space-y-2">
- {originalDeletionAction !== undefined &&
- originalDeletionAction !== updatedDeletionAction && (
- <Badge color="gray" className="line-through">
- On delete: {originalDeletionAction}
- </Badge>
- )}
- {updatedDeletionAction !== undefined && (
- <div>
- <Badge color="green">On delete: {updatedDeletionAction}</Badge>
- </div>
- )}
- </div>
+ </div>
+ <div className="flex items-center space-x-2">
+ {originalDeletionAction !== updatedDeletionAction && (
+ <>
+ <Badge color="gray" className="line-through">
+ On delete: {originalDeletionAction ?? 'No action'}
+ </Badge>
+ <Badge color="green">On delete: {updatedDeletionAction ?? 'No action'}</Badge>
+ </>
+ )}
+ </div>
+ <div className="flex items-center space-x-2">
+ {originalUpdateAction !== updatedUpdateAction && (
+ <>
+ <Badge color="gray" className="line-through">
+ On update: {originalUpdateAction ?? 'No action'}
+ </Badge>
+ <Badge color="green">On update: {updatedUpdateAction ?? 'No action'}</Badge>
+ </>
+ )}
</div>
</div>
<div className="flex items-center space-x-2">
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx
index 12e141860ed..9cbc393762f 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx
@@ -21,7 +21,7 @@ import ActionBar from '../ActionBar'
import { ColumnField } from '../SidePanelEditor.types'
import { FOREIGN_KEY_CASCADE_OPTIONS } from './ForeignKeySelector.constants'
import { ForeignKey } from './ForeignKeySelector.types'
-import { generateDeletionActionDescription } from './ForeignKeySelector.utils'
+import { generateCascadeActionDescription } from './ForeignKeySelector.utils'
interface ForeignKeySelectorProps {
column: ColumnField
@@ -29,7 +29,14 @@ interface ForeignKeySelectorProps {
visible: boolean
closePanel: () => void
saveChanges: (
- value: { table: PostgresTable; column: PostgresColumn; deletionAction: string } | undefined
+ value:
+ | {
+ table: PostgresTable
+ column: PostgresColumn
+ deletionAction: string
+ updateAction: string
+ }
+ | undefined
) => void
}
@@ -46,6 +53,7 @@ const ForeignKeySelector = ({
table: '',
column: '',
deletionAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
+ updateAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
})
const schemas = meta.schemas.list()
@@ -78,6 +86,7 @@ const ForeignKeySelector = ({
table: foreignKey.target_table_name,
column: foreignKey.target_column_name,
deletionAction: foreignKey.deletion_action,
+ updateAction: foreignKey.update_action,
})
} else {
setSelectedForeignKey({
@@ -85,6 +94,7 @@ const ForeignKeySelector = ({
table: '',
column: '',
deletionAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
+ updateAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
})
}
}
@@ -97,6 +107,7 @@ const ForeignKeySelector = ({
table: '',
column: '',
deletionAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
+ updateAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
}
setSelectedForeignKey(updatedForeignKey)
}
@@ -104,13 +115,15 @@ const ForeignKeySelector = ({
const updateSelectedTable = (tableId: number) => {
setErrors({})
if (!tableId) {
- setSelectedForeignKey({
+ return setSelectedForeignKey({
schema: '',
table: '',
column: '',
deletionAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
+ updateAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
})
}
+
const table = find(tables, { id: tableId })
if (table) {
const primaryColumn = table.primary_keys[0].name
@@ -121,6 +134,7 @@ const ForeignKeySelector = ({
table: table.name,
column: primaryColumn ?? firstColumn,
deletionAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
+ updateAction: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
})
}
}
@@ -134,9 +148,9 @@ const ForeignKeySelector = ({
}
}
- const updateDeletionAction = (value: string) => {
+ const updateCascadeAction = (action: 'updateAction' | 'deletionAction', value: string) => {
setErrors({})
- setSelectedForeignKey({ ...selectedForeignKey, deletionAction: value })
+ setSelectedForeignKey({ ...selectedForeignKey, [action]: value })
}
const onSaveChanges = (resolve: () => void) => {
@@ -157,6 +171,7 @@ const ForeignKeySelector = ({
table: selectedTable,
column: selectedColumn,
deletionAction: selectedForeignKey.deletionAction,
+ updateAction: selectedForeignKey.updateAction,
})
}
}
@@ -324,7 +339,9 @@ const ForeignKeySelector = ({
</AlertDescription_Shadcn_>
</Alert_Shadcn_>
)}
+
<SidePanel.Separator />
+
<InformationBox
icon={<IconHelpCircle size="large" strokeWidth={1.5} />}
title="Which action is most appropriate?"
@@ -363,20 +380,23 @@ const ForeignKeySelector = ({
<Listbox
id="updateAction"
- value={selectedForeignKey.deletionAction}
+ value={selectedForeignKey.updateAction}
label="Action if referenced row is updated"
descriptionText={
<p>
- {generateDeletionActionDescription(
- selectedForeignKey.deletionAction,
+ {generateCascadeActionDescription(
+ 'update',
+ selectedForeignKey.updateAction,
`${selectedForeignKey.schema}.${selectedForeignKey.table}`
)}
</p>
}
error={errors.column}
- onChange={(value: string) => updateDeletionAction(value)}
+ onChange={(value: string) => updateCascadeAction('updateAction', value)}
>
- {FOREIGN_KEY_CASCADE_OPTIONS.map((option) => (
+ {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-scale-1200">{option.label}</p>
</Listbox.Option>
@@ -390,7 +410,8 @@ const ForeignKeySelector = ({
descriptionText={
<>
<p>
- {generateDeletionActionDescription(
+ {generateCascadeActionDescription(
+ 'delete',
selectedForeignKey.deletionAction,
`${selectedForeignKey.schema}.${selectedForeignKey.table}`
)}
@@ -408,7 +429,7 @@ const ForeignKeySelector = ({
</>
}
error={errors.column}
- onChange={(value: string) => updateDeletionAction(value)}
+ onChange={(value: string) => updateCascadeAction('deletionAction', value)}
>
{FOREIGN_KEY_CASCADE_OPTIONS.map((option) => (
<Listbox.Option key={option.key} value={option.value} label={option.label}>
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.types.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.types.ts
index 3451d63707f..7dcc9d8d1fe 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.types.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.types.ts
@@ -3,4 +3,5 @@ export interface ForeignKey {
table: string
column?: string
deletionAction: string
+ updateAction: string
}
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.utils.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.utils.tsx
index 42632668542..7b281a0b00d 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.utils.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.utils.tsx
@@ -1,16 +1,21 @@
import * as Tooltip from '@radix-ui/react-tooltip'
import { FOREIGN_KEY_CASCADE_ACTION } from 'data/database/database-query-constants'
import { IconHelpCircle } from 'ui'
-import { getForeignKeyDeletionAction } from '../ColumnEditor/ColumnEditor.utils'
+import { getForeignKeyCascadeAction } from '../ColumnEditor/ColumnEditor.utils'
-export const generateDeletionActionDescription = (deletionAction: string, reference: string) => {
- const actionName = getForeignKeyDeletionAction(deletionAction) ?? 'No action'
+export const generateCascadeActionDescription = (
+ action: 'update' | 'delete',
+ cascadeAction: string,
+ reference: string
+) => {
+ const actionVerb = action === 'update' ? 'Updating' : 'Deleting'
+ const actionName = getForeignKeyCascadeAction(cascadeAction) ?? 'No action'
- switch (deletionAction) {
+ switch (cascadeAction) {
case FOREIGN_KEY_CASCADE_ACTION.NO_ACTION:
return (
<>
- <span className="text-scale-1100">{actionName}</span>: Deleting a record from{' '}
+ <span className="text-scale-1100">{actionName}</span>: {actionVerb} a record from{' '}
<code className="text-xs text-scale-1100">{reference}</code> will{' '}
<span className="text-amber-900 opacity-75">raise an error</span> if there are records
existing in this table that reference it
@@ -19,10 +24,10 @@ export const generateDeletionActionDescription = (deletionAction: string, refere
case FOREIGN_KEY_CASCADE_ACTION.CASCADE:
return (
<>
- <span className="text-scale-1100">{actionName}</span>: Deleting a record from{' '}
+ <span className="text-scale-1100">{actionName}</span>: {actionVerb} a record from{' '}
<code className="text-xs text-scale-1100">{reference}</code> will{' '}
- <span className="text-amber-900 opacity-75">also delete</span> any records that reference
- it in this table
+ <span className="text-amber-900 opacity-75">also {action}</span> any records that
+ reference it in this table
</>
)
case FOREIGN_KEY_CASCADE_ACTION.RESTRICT:
@@ -50,15 +55,15 @@ export const generateDeletionActionDescription = (deletionAction: string, refere
</Tooltip.Content>
</Tooltip.Portal>
</Tooltip.Root>
- : Deleting a record from <code className="text-xs text-scale-1100">{reference}</code> will{' '}
- <span className="text-amber-900 opacity-75">prevent deletion</span> of existing
- referencing rows from this table.
+ : {actionVerb} a record from <code className="text-xs text-scale-1100">{reference}</code>{' '}
+ will <span className="text-amber-900 opacity-75">prevent {actionVerb.toLowerCase()}</span>{' '}
+ existing referencing rows from this table.
</>
)
case FOREIGN_KEY_CASCADE_ACTION.SET_DEFAULT:
return (
<>
- <span className="text-scale-1100">{actionName}</span>: Deleting a record from{' '}
+ <span className="text-scale-1100">{actionName}</span>: {actionVerb} a record from{' '}
<code className="text-xs text-scale-1100">{reference}</code> will set the value of any
existing records in this table referencing it to their{' '}
<span className="text-amber-900 opacity-75">default value</span>
@@ -67,7 +72,7 @@ export const generateDeletionActionDescription = (deletionAction: string, refere
case FOREIGN_KEY_CASCADE_ACTION.SET_NULL:
return (
<>
- <span className="text-scale-1100">{actionName}</span>: Deleting a record from{' '}
+ <span className="text-scale-1100">{actionName}</span>: {actionVerb} a record from{' '}
<code className="text-xs text-scale-1100">{reference}</code> will set the value of any
existing records in this table referencing it{' '}
<span className="text-amber-900 opacity-75">to NULL</span>
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts
index 392a449c00f..4cdcf5fd736 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.types.ts
@@ -59,6 +59,7 @@ export interface Field {
export interface ExtendedPostgresRelationship extends PostgresRelationship {
deletion_action: string
+ update_action: string
}
export interface ColumnField {
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx
index cf1a706457f..031ac09b0ed 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/Column.tsx
@@ -16,7 +16,7 @@ import { FOREIGN_KEY_CASCADE_ACTION } from 'data/database/database-query-constan
import { noop } from 'lodash'
import { typeExpressionSuggestions } from '../ColumnEditor/ColumnEditor.constants'
import { Suggestion } from '../ColumnEditor/ColumnEditor.types'
-import { getForeignKeyDeletionAction } from '../ColumnEditor/ColumnEditor.utils'
+import { getForeignKeyCascadeAction } from '../ColumnEditor/ColumnEditor.utils'
import ColumnType from '../ColumnEditor/ColumnType'
import InputWithSuggestions from '../ColumnEditor/InputWithSuggestions'
import { ColumnField } from '../SidePanelEditor.types'
@@ -129,7 +129,7 @@ const Column = ({
</div>
{column.foreignKey.deletion_action !== FOREIGN_KEY_CASCADE_ACTION.NO_ACTION && (
<p className="text-xs text-scale-1200 mt-1">
- On delete: {getForeignKeyDeletionAction(column.foreignKey.deletion_action)}
+ On delete: {getForeignKeyCascadeAction(column.foreignKey.deletion_action)}
</p>
)}
</div>
diff --git a/studio/data/database/foreign-key-constraints-query.ts b/studio/data/database/foreign-key-constraints-query.ts
index 4a218079e88..e6cd657c733 100644
--- a/studio/data/database/foreign-key-constraints-query.ts
+++ b/studio/data/database/foreign-key-constraints-query.ts
@@ -10,6 +10,7 @@ export type ForeignKeyConstraint = {
id: number
constraint_name: string
deletion_action: string
+ update_action: string
source_id: string
source_schema: string
source_table: string
@@ -25,7 +26,8 @@ export const getForeignKeyConstraintsQuery = ({ schema }: GetForeignKeyConstrain
SELECT
con.oid as id,
con.conname as constraint_name,
- con.confdeltype as deletion_action,
+ con.confdeltype as deletion_action,
+ con.confupdtype as update_action,
rel.oid as source_id,
nsp.nspname as source_schema,
rel.relname as source_table,
diff --git a/studio/stores/pgmeta/MetaStore.ts b/studio/stores/pgmeta/MetaStore.ts
index b859c06d116..3a012c558b6 100644
--- a/studio/stores/pgmeta/MetaStore.ts
+++ b/studio/stores/pgmeta/MetaStore.ts
@@ -302,7 +302,7 @@ export default class MetaStore implements IMetaStore {
// [Joshen TODO] Eventually need to extend this to composite foreign keys
async addForeignKey(relationship: ExtendedPostgresRelationship) {
- const { deletion_action } = relationship
+ const { deletion_action, update_action } = relationship
const deletionAction =
deletion_action === FOREIGN_KEY_CASCADE_ACTION.CASCADE
? 'ON DELETE CASCADE'
@@ -313,12 +313,20 @@ export default class MetaStore implements IMetaStore {
: deletion_action === FOREIGN_KEY_CASCADE_ACTION.SET_NULL
? 'ON DELETE SET NULL'
: ''
+ const updateAction =
+ update_action === FOREIGN_KEY_CASCADE_ACTION.CASCADE
+ ? 'ON UPDATE CASCADE'
+ : update_action === FOREIGN_KEY_CASCADE_ACTION.RESTRICT
+ ? 'ON UPDATE RESTRICT'
+ : ''
const query = `
ALTER TABLE "${relationship.source_schema}"."${relationship.source_table_name}"
ADD CONSTRAINT "${relationship.source_table_name}_${relationship.source_column_name}_fkey"
FOREIGN KEY ("${relationship.source_column_name}")
- REFERENCES "${relationship.target_table_schema}"."${relationship.target_table_name}" ("${relationship.target_column_name}") ${deletionAction};
+ REFERENCES "${relationship.target_table_schema}"."${relationship.target_table_name}" ("${relationship.target_column_name}")
+ ${updateAction}
+ ${deletionAction};
`
.replace(/\s+/g, ' ')
.trim()
@@ -563,6 +571,7 @@ export default class MetaStore implements IMetaStore {
...relationship,
source_table_name: duplicatedTableName,
deletion_action: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
+ update_action: FOREIGN_KEY_CASCADE_ACTION.NO_ACTION,
})
if (relation.error) throw relation.error
})