Skip to content

Commit 6f860fc

Browse files
fix(tables): refetch rows when a select option is removed
Removing an option rewrites cells server-side, so the schema-only invalidation left the cache holding orphaned ids — hidden by the grid but still visible to emptiness checks, filters, and dependent-group eligibility.
1 parent 6ab46ed commit 6f860fc

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

apps/sim/hooks/queries/tables.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,24 @@ export function useDeleteTableRowsAsync({ workspaceId, tableId }: RowMutationCon
11821182

11831183
type UpdateColumnParams = Omit<UpdateTableColumnBodyInput, 'workspaceId'>
11841184

1185+
/**
1186+
* Whether an update drops a `select` option the column currently declares —
1187+
* the one options edit the server answers by rewriting cells.
1188+
*/
1189+
function removesSelectOption(
1190+
previousDetail: TableDefinition | undefined,
1191+
{ columnName, updates }: UpdateColumnParams
1192+
): boolean {
1193+
if (updates.options === undefined || previousDetail === undefined) return false
1194+
const lower = columnName.toLowerCase()
1195+
const column = previousDetail.schema.columns.find(
1196+
(c) => getColumnId(c) === columnName || c.name.toLowerCase() === lower
1197+
)
1198+
if (!column?.options?.length) return false
1199+
const keptIds = new Set(updates.options.map((o) => o.id))
1200+
return column.options.some((o) => !keptIds.has(o.id))
1201+
}
1202+
11851203
/**
11861204
* Update a column (rename, type change, or constraint update).
11871205
*/
@@ -1227,13 +1245,17 @@ export function useUpdateColumn({ workspaceId, tableId }: RowMutationContext) {
12271245
if (isValidationError(error)) return
12281246
toast.error(error.message, { duration: 5000 })
12291247
},
1230-
onSettled: (_data, _error, variables) => {
1231-
// A type change, or a select single↔multi toggle, rewrites stored cells
1232-
// server-side (option ids ↔ names, scalar ↔ array). Those need the rows
1233-
// refetched too — the schema-only path would leave the grid rendering the
1234-
// pre-migration values. Everything else really is metadata-only.
1248+
onSettled: (_data, _error, variables, context) => {
1249+
// A type change, a select single↔multi toggle, or removing an option
1250+
// rewrites stored cells server-side (option ids ↔ names, scalar ↔ array,
1251+
// removed ids cleared). Those need the rows refetched too — the
1252+
// schema-only path would leave the cache holding pre-migration values,
1253+
// which the grid hides but emptiness checks, filters, and dependent-group
1254+
// eligibility still act on. Everything else really is metadata-only.
12351255
const rewritesRows =
1236-
variables.updates.type !== undefined || variables.updates.multiple !== undefined
1256+
variables.updates.type !== undefined ||
1257+
variables.updates.multiple !== undefined ||
1258+
removesSelectOption(context?.previousDetail, variables)
12371259
if (rewritesRows) invalidateTableSchema(queryClient, tableId)
12381260
else invalidateTableSchemaOnly(queryClient, tableId)
12391261
},

0 commit comments

Comments
 (0)