toeverything/AFFiNE · error · Error

Record with key ${key} already exists in table ${this.tableN

Error message

Record with key ${key} already exists in table ${this.tableName}

What it means

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.

Source

Thrown at packages/common/infra/src/orm/core/adapters/memory/table.ts:35

export class MemoryTableAdapter implements TableAdapter {
  private readonly data = new Map<string, any>();
  private keyField = 'key';
  private readonly subscriptions = new Set<(key: string, data: any) => void>();

  constructor(private readonly tableName: string) {}

  setup(opts: TableAdapterOptions) {
    this.keyField = opts.keyField;
  }

  dispose() {}

  insert(query: InsertQuery) {
    const { data, select } = query;
    const key = String(data[this.keyField]);

    if (this.data.has(key)) {
      throw new Error(
        `Record with key ${key} already exists in table ${this.tableName}`
      );
    }

    this.data.set(key, data);
    this.dispatch(key, data);
    return this.value(data, select);
  }

  find(query: FindQuery) {
    const { where, select } = query;
    const result = [];

    for (const record of this.iterate(where)) {
      result.push(this.value(record, select));
    }

    return result;

View on GitHub (pinned to b4c8548c09)

Solutions

  1. Use update instead of insert when the record key already exists in the table.
  2. Check existence first and handle the duplicate key path explicitly.
  3. Ensure the primary key generation does not produce colliding keys.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/common/infra/src/orm/core/adapters/memory/table.ts:35 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of toeverything/AFFiNE@b4c8548c09 (2026-08-18). Data as JSON: /api/errors/b2efad1efdba1d1e. Report an issue: GitHub.