Skip to content

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

When creating a foreign key, always default to the primary key in column dropdown of the referenced table.

Ivan Vasilovebdcaca6e071
diff --git a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx
index 3a6fb96cc55..65871c80afd 100644
--- a/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx
+++ b/studio/components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.tsx
@@ -1,16 +1,16 @@
-import React, { FC, useEffect, useState } from 'react'
-import { get, find, isEmpty, sortBy } from 'lodash'
+import type { PostgresColumn, PostgresSchema, PostgresTable } from '@supabase/postgres-meta'
import { Dictionary } from 'components/grid'
-import { SidePanel, Input, Listbox, IconHelpCircle, IconDatabase, Toggle } from 'ui'
-import type { PostgresTable, PostgresColumn, PostgresSchema } from '@supabase/postgres-meta'
+import { find, get, isEmpty, sortBy } from 'lodash'
+import { FC, useEffect, useState } from 'react'
+import { IconDatabase, IconHelpCircle, Input, Listbox, SidePanel } from 'ui'
+import InformationBox from 'components/ui/InformationBox'
+import { FOREIGN_KEY_DELETION_ACTION } from 'data/database/database-query-constants'
import { useStore } from 'hooks'
import ActionBar from '../ActionBar'
-import { ForeignKey } from './ForeignKeySelector.types'
import { ColumnField } from '../SidePanelEditor.types'
-import InformationBox from 'components/ui/InformationBox'
-import { FOREIGN_KEY_DELETION_ACTION } from 'data/database/database-query-constants'
import { FOREIGN_KEY_DELETION_OPTIONS } from './ForeignKeySelector.constants'
+import { ForeignKey } from './ForeignKeySelector.types'
import { generateDeletionActionDescription } from './ForeignKeySelector.utils'
interface Props {
@@ -98,10 +98,13 @@ const ForeignKeySelector: FC<Props> = ({ column, visible = false, closePanel, sa
}
const table = find(tables, { id: tableId })
if (table) {
+ const primaryColumn = table.primary_keys[0].name
+ const firstColumn = table.columns?.length ? table.columns[0].name : undefined
+
setSelectedForeignKey({
schema: table.schema,
table: table.name,
- column: table.columns?.length ? table.columns[0].name : undefined,
+ column: primaryColumn ?? firstColumn,
deletionAction: FOREIGN_KEY_DELETION_ACTION.NO_ACTION,
})
}