{"record":{"id":"0237ca96c154806a","repo":"hcengineering/platform","slug":"peer-value-already-exists","errorCode":null,"errorMessage":"Peer value already exists","messagePattern":"Peer value already exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"foundations/communication/packages/cockroach/src/db/peer.ts","lineNumber":59,"sourceCode":"    const db: DbModel<Domain.Peer> = {\n      workspace_id: workspaceId,\n      card_id: cardId,\n      kind,\n      value,\n      extra,\n      created: date\n    }\n\n    if (options?.newValue === true) {\n      const { sql, values } = this.getInsertSql(Domain.Peer, db, [], {\n        conflictColumns: ['workspace_id', 'kind', 'value'],\n        conflictAction: 'DO NOTHING'\n      })\n      const result = await this.execute(sql, values, 'insert peer')\n      const count = result?.count ?? 0\n\n      if (count === 0) {\n        throw Error('Peer value already exists')\n      }\n    } else {\n      const { sql, values } = this.getInsertSql(Domain.Peer, db, [])\n      await this.execute(sql, values, 'insert peer')\n    }\n  }\n\n  async removePeer (workspaceId: WorkspaceUuid, cardId: CardID, kind: PeerKind, value: string): Promise<void> {\n    const filter: DbModelFilter<Domain.Peer> = [\n      {\n        column: 'workspace_id',\n        value: workspaceId\n      },\n      {\n        column: 'card_id',\n        value: cardId\n      },\n      {","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/foundations/communication/packages/cockroach/src/db/peer.ts#L41-L77","documentation":"createPeer in the CockroachDB implementation performs an INSERT ... ON CONFLICT DO NOTHING and then checks the affected row count. If zero rows were inserted, a peer record with the same key already exists, and it throws this plain Error. The method enforces peer uniqueness rather than silently overwriting or ignoring duplicates.","triggerScenarios":"Calling createPeer (foundations/communication/packages/cockroach/src/db/peer.ts:59) when an INSERT into the Peer domain is a no-op due to ON CONFLICT DO NOTHING — i.e., a peer with the same unique key/value already exists in the table.","commonSituations":"Concurrent createPeer calls racing to insert the same peer (only one wins, the loser gets this error); retry logic re-invoking createPeer after a timeout when the first call actually succeeded; node re-registration without removing the old peer row; idempotency handling that assumes createPeer is safe to call twice.","solutions":["Check existence first (query the peer by key) and skip the insert if it already exists","Wrap createPeer in try-catch and treat 'Peer value already exists' as success for idempotent re-registration","Delete/upsert the existing peer row first (or use ON CONFLICT DO UPDATE) if replacing is intended","Serialize creation (e.g., per-peer lock) to avoid concurrent duplicate inserts"],"exampleFix":"// before\ntry { await db.createPeer(peer) } catch (e) { /* crash */ }\n// after\ntry {\n  await db.createPeer(peer)\n} catch (e) {\n  if (!(e instanceof Error && e.message === 'Peer value already exists')) throw e\n  // treat as success: peer already registered\n}","handlingStrategy":"try-catch","validationCode":"const existing = await db.findPeer(peer.key)\nif (existing) return existing // skip createPeer entirely","typeGuard":"function isDuplicatePeerError(e: unknown): e is Error {\n  return e instanceof Error && e.message === 'Peer value already exists'\n}","tryCatchPattern":"try {\n  await db.createPeer(peer)\n} catch (e) {\n  if (isDuplicatePeerError(e)) return // idempotent re-registration\n  throw e\n}","preventionTips":["Treat createPeer as non-idempotent; check existence or catch duplicates on retry","Avoid concurrent createPeer calls for the same key (use per-key serialization)","Delete or upsert old peer rows before re-registering a node","Add retry logic that deduplicates on this specific error message"],"tags":["database","cockroachdb","duplicate","concurrency"],"backgroundTag":"duplicate-record-conflict","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}