{"record":{"id":"b2efad1efdba1d1e","repo":"toeverything/AFFiNE","slug":"record-with-key-key-already-exists-in-table-t","errorCode":null,"errorMessage":"Record with key ${key} already exists in table ${this.tableName}","messagePattern":"Record with key (.+?) already exists in table (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/common/infra/src/orm/core/adapters/memory/table.ts","lineNumber":35,"sourceCode":"export class MemoryTableAdapter implements TableAdapter {\n  private readonly data = new Map<string, any>();\n  private keyField = 'key';\n  private readonly subscriptions = new Set<(key: string, data: any) => void>();\n\n  constructor(private readonly tableName: string) {}\n\n  setup(opts: TableAdapterOptions) {\n    this.keyField = opts.keyField;\n  }\n\n  dispose() {}\n\n  insert(query: InsertQuery) {\n    const { data, select } = query;\n    const key = String(data[this.keyField]);\n\n    if (this.data.has(key)) {\n      throw new Error(\n        `Record with key ${key} already exists in table ${this.tableName}`\n      );\n    }\n\n    this.data.set(key, data);\n    this.dispatch(key, data);\n    return this.value(data, select);\n  }\n\n  find(query: FindQuery) {\n    const { where, select } = query;\n    const result = [];\n\n    for (const record of this.iterate(where)) {\n      result.push(this.value(record, select));\n    }\n\n    return result;","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/b4c8548c09da21b2898443559a5b846f0ccf5dd8/packages/common/infra/src/orm/core/adapters/memory/table.ts#L17-L53","documentation":"MemoryTableAdapter.insert enforces primary-key uniqueness in the in-memory store: if the Map already contains the key derived from data[keyField], the insert conflicts and this Error is thrown naming the key and table.","triggerScenarios":"Thrown at packages/common/infra/src/orm/core/adapters/memory/table.ts:35 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use update instead of insert when the record key already exists in the table.","Check existence first and handle the duplicate key path explicitly.","Ensure the primary key generation does not produce colliding keys."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"b4c8548c09da21b2898443559a5b846f0ccf5dd8","analyzedAt":"2026-08-18T21:16:52.546Z","contentChangedAt":"2026-08-18T21:16:52.546Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}