Skip to content

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

fix: omit cleared default fields on row insert (#46826)

Aaditya Bhusalbbe5afff61cf
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
index 198ffdef173..ced4a6bc70a 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.tsx
@@ -133,7 +133,7 @@ export const RowEditor = ({
updateEditorDirty()
const payload = isNewRecord
- ? generateRowObjectFromFields({ fields: rowFields })
+ ? generateRowObjectFromFields({ fields: rowFields, useDefaultForEmptyValues: true })
: generateUpdateRowPayload(row, rowFields)
const configuration = { identifiers: {}, rowIdx: -1 }
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.test.ts b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.test.ts
index da3ab85c0c8..4735666c5ab 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.test.ts
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.test.ts
@@ -176,6 +176,51 @@ describe('generateRowObjectFromFields', () => {
const result = generateRowObjectFromFields({ fields: sampleRowFields })
expect(result).toEqual({ name: '' })
})
+ it('should omit cleared identity and default fields for new rows', () => {
+ const sampleRowFields: RowField[] = [
+ {
+ id: '1',
+ name: 'id',
+ value: '',
+ comment: '',
+ defaultValue: null,
+ format: 'int8',
+ enums: [],
+ isNullable: false,
+ isIdentity: true,
+ isPrimaryKey: true,
+ },
+ {
+ id: '2',
+ name: 'created_at',
+ value: '',
+ comment: '',
+ defaultValue: 'now()',
+ format: 'timestamptz',
+ enums: [],
+ isNullable: false,
+ isIdentity: false,
+ isPrimaryKey: false,
+ },
+ {
+ id: '3',
+ name: 'name',
+ value: '',
+ comment: '',
+ defaultValue: null,
+ format: 'text',
+ enums: [],
+ isNullable: false,
+ isIdentity: false,
+ isPrimaryKey: false,
+ },
+ ]
+ const result = generateRowObjectFromFields({
+ fields: sampleRowFields,
+ useDefaultForEmptyValues: true,
+ })
+ expect(result).toEqual({ name: '' })
+ })
it('should discern NULL values for text', () => {
const sampleRowFields: RowField[] = [
{
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
index 1837ede1343..d9e83aae425 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
@@ -191,16 +191,25 @@ const convertInputDatetimeToPostgresDatetime = (
export const generateRowObjectFromFields = ({
fields,
includeUndefinedValues = false,
+ useDefaultForEmptyValues = false,
}: {
fields: RowField[]
includeUndefinedValues?: boolean
+ useDefaultForEmptyValues?: boolean
}): object => {
const rowObject = {} as any
fields.forEach((field) => {
const isArray = field.format.startsWith('_')
const value = field.value
+ const shouldUseDefaultValue =
+ useDefaultForEmptyValues &&
+ value === '' &&
+ (field.isIdentity || field.defaultValue !== null) &&
+ !TEXT_TYPES.includes(field.format)
- if (isArray && value !== null) {
+ if (shouldUseDefaultValue) {
+ rowObject[field.name] = undefined
+ } else if (isArray && value !== null) {
rowObject[field.name] = tryParseJson(value)
} else if (field.format.includes('json')) {
if (typeof field.value === 'object') {