{"record":{"id":"505c6f60b9795c5a","repo":"toeverything/AFFiNE","slug":"databaseblockerror-505c6f","errorCode":"DatabaseBlockError","errorMessage":"can't resolve ref type","messagePattern":"can't resolve ref type","errorType":"exception","errorClass":"BlockSuiteError","httpStatus":null,"severity":"error","filePath":"blocksuite/affine/data-view/src/core/filter/utils.ts","lineNumber":11,"sourceCode":"import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';\n\nimport { getRefType } from '../expression/ref/ref.js';\nimport type { Variable, VariableRef } from '../expression/types.js';\nimport { filterMatcher } from './filter-fn/matcher.js';\nimport type { FilterGroup, SingleFilter } from './types.js';\n\nexport const firstFilterName = (vars: Variable[], ref: VariableRef) => {\n  const type = getRefType(vars, ref);\n  if (!type) {\n    throw new BlockSuiteError(\n      ErrorCode.DatabaseBlockError,\n      `can't resolve ref type`\n    );\n  }\n  return filterMatcher.firstMatchedBySelfType(type)?.name;\n};\nexport const firstFilterByRef = (\n  vars: Variable[],\n  ref: VariableRef\n): SingleFilter => {\n  return {\n    type: 'filter',\n    left: ref,\n    function: firstFilterName(vars, ref),\n    args: [],\n  };\n};\nexport const firstFilter = (vars: Variable[]): SingleFilter => {","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/b4c8548c09da21b2898443559a5b846f0ccf5dd8/blocksuite/affine/data-view/src/core/filter/utils.ts#L1-L29","documentation":"Thrown by firstFilterName (blocksuite/affine/data-view/src/core/filter/utils.ts:11) when getRefType(vars, ref) returns undefined. A filter's left-hand VariableRef must resolve to a Variable in the passed vars list; if no variable matches ref.name (a column id), the type cannot be determined and DatabaseBlockError 'can't resolve ref type' is thrown. It signals a filter expression referencing a column that is not in the current variable set.","triggerScenarios":"Calling firstFilterName or firstFilterByRef with a ref whose name is a deleted column id; passing a filtered/subset vars array that excludes the referenced column; deserializing saved filter JSON whose left.name no longer matches any dataSource property id.","commonSituations":"Loading a saved view whose filter references a column deleted later; copying filter config between databases with different schemas; column deletion racing the filter menu re-render.","solutions":["Validate before calling: only build the filter if vars.some(v => v.id === ref.name) and getRefType returns a type","Repair saved filters on load: remove or rebuild filters whose left.name is absent from the current variable ids","When deleting a column, also delete filters (and sorts/group-bys) that reference it","Catch the error and rebuild with firstFilter(vars) against an existing column"],"exampleFix":"// before\nconst filter = firstFilterByRef(vars, { type: 'ref', name: deletedColumnId }); // throws\n\n// after\nif (!vars.some(v => v.id === ref.name)) {\n  // rebuild against an existing column instead of throwing\n  return firstFilter(vars);\n}","handlingStrategy":"validation","validationCode":"import { getRefType } from '../expression/ref/ref.js';\n\nconst isRefResolvable = (vars: Variable[], ref: VariableRef): boolean =>\n  vars.some(v => v.id === ref.name) && !!getRefType(vars, ref);","typeGuard":"const isResolvableRef = (vars: Variable[]): ((ref: VariableRef) => boolean) =>\n  ref => vars.some(v => v.id === ref.name);","tryCatchPattern":"try {\n  const filter = firstFilterByRef(vars, ref);\n} catch (e) {\n  if (e instanceof BlockSuiteError && e.code === BlockSuiteError.ErrorCode.DatabaseBlockError) {\n    // referenced column is gone: rebuild a default filter from remaining vars\n    return firstFilter(vars);\n  }\n  throw e;\n}","preventionTips":["When deleting a column, delete filters referencing it in the same update","Validate saved filter JSON against current column ids on load","Never pass a subsetted vars array that excludes the ref target"],"tags":["database-block","filter","column-reference","stale-id"],"backgroundTag":"unresolved-type-reference","analyzedSha":"b4c8548c09da21b2898443559a5b846f0ccf5dd8","analyzedAt":"2026-08-18T21:16:52.546Z","contentChangedAt":"2026-08-18T21:16:52.546Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}