{"record":{"id":"99e6049af45b7efd","repo":"twentyhq/twenty","slug":"failed-to-persist-searchfieldmetadata-rows-for-wor","errorCode":null,"errorMessage":"Failed to persist searchFieldMetadata rows for workspace ${workspaceId}","messagePattern":"Failed to persist searchFieldMetadata rows for workspace (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/twenty-server/src/database/commands/upgrade-version-command/2-16/2-16-workspace-command-1799100000000-backfill-search-field-metadata.command.ts","lineNumber":154,"sourceCode":"                flatEntityToDelete: [],\n                flatEntityToUpdate: [],\n              },\n            },\n            workspaceId,\n            applicationUniversalIdentifier,\n          },\n        );\n\n      if (validateAndBuildResult.status === 'fail') {\n        this.logger.error(\n          `Failed to persist searchFieldMetadata rows for application ${applicationUniversalIdentifier}:\\n${JSON.stringify(\n            validateAndBuildResult,\n            null,\n            2,\n          )}`,\n        );\n\n        throw new Error(\n          `Failed to persist searchFieldMetadata rows for workspace ${workspaceId}`,\n        );\n      }\n    }\n\n    this.logger.log(\n      `Successfully backfilled ${totalRowsToCreate} searchFieldMetadata row(s) for workspace ${workspaceId}`,\n    );\n  }\n}\n","sourceCodeStart":136,"sourceCodeEnd":165,"githubUrl":"https://github.com/twentyhq/twenty/blob/1f5dd2bbd2a8da3419c8cfd52dd545c0024df1a6/packages/twenty-server/src/database/commands/upgrade-version-command/2-16/2-16-workspace-command-1799100000000-backfill-search-field-metadata.command.ts#L136-L165","documentation":"Thrown by the 2.16 backfill-search-field-metadata command when validateAndBuildResult.status === 'fail' while persisting searchFieldMetadata rows for an application. The logger.error above prints per-application context; the thrown error is workspace-scoped. searchFieldMetadata drives full-text search indexing, so a build failure here means the search payload did not pass validation.","triggerScenarios":"Running the 2.16 upgrade where the application's fieldMetadata referenced by the search rows is missing/inactive, or the flatEntityToCreate payload references a fieldMetadataId that the validator cannot resolve; happens per-application inside the loop, so the offending applicationUniversalIdentifier is in the log line just above.","commonSituations":"Application with deleted/deactivated fields whose search rows still try to reference them; image whose standard-application seed does not match the workspace; partial prior run leaving duplicate searchFieldMetadata rows.","solutions":["Read the per-application logger.error line to find which applicationUniversalIdentifier failed.","Inspect the JSON in that log to see which searchFieldMetadata rows failed and why.","Verify the referenced fieldMetadata rows exist and are active for that application; reactivate or repoint them.","Re-run the workspace upgrade for that workspace."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// For each application, confirm referenced fieldMetadata rows exist and are active\n// before persisting searchFieldMetadata rows.\nconst dangling = await dataSource.query(`\n  SELECT s.\"id\" FROM core.\"searchFieldMetadata\" s\n  LEFT JOIN core.\"fieldMetadata\" f ON f.\"id\" = s.\"fieldMetadataId\"\n  WHERE f.\"id\" IS NULL OR f.\"isActive\" = false\n`);\nif (dangling.length) throw new Error(`Dangling search refs: ${JSON.stringify(dangling)}`);","typeGuard":"function isFailResult(r: unknown): r is { status: 'fail' } {\n  return typeof r === 'object' && r !== null && (r as any).status === 'fail';\n}","tryCatchPattern":"for (const app of apps) {\n  try {\n    const res = await service.validateBuildAndRunLegacyWorkspaceMigration(payloadFor(app));\n    if (res.status === 'fail') throw new Error(`... for workspace ${workspaceId}`);\n  } catch (err) { upgradeAudit.record(workspaceId, app.id, err); throw err; }\n}","preventionTips":["Keep application fieldMetadata and searchFieldMetadata consistent; never hard-delete one without the other.","Deactivate rather than delete when fields have search rows.","Run upgrades with API mutations paused."],"tags":["migration","workspace-upgrade","metadata-validation","search","backfill"],"backgroundTag":null,"analyzedSha":"1f5dd2bbd2a8da3419c8cfd52dd545c0024df1a6","analyzedAt":"2026-08-12T15:37:27.593Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}