toeverything/AFFiNE · error · Error

[Table( )]: Primary key field ' ' is required but not set.

Error message

[Table(${table.name})]: Primary key field '${table.keyField}' is required but not set.

What it means

A data validator (PrimaryKeyShouldExist) run on insert/create paths: it fires when the record being written has its primary-key field (table.keyField) set to undefined or null. Every row must carry a primary key, so the faulting input is the data object passed to the ORM operation, which is missing table.keyField for the named table.

Solutions

  1. Set the primary key field on the entity before inserting it into the table.
  2. Provide a default value provider for the key field in the schema if it should be generated.
  3. Check the insert code path for a missing key assignment.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/common/infra/src/orm/core/validators/data.ts:36 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/671be70dd6c1e0df. Report an issue: GitHub.

Appendix: source

Thrown at packages/common/infra/src/orm/core/validators/data.ts:36

      case 'bigint':
      case 'function':
      case 'object': // we've already converted available types into 'json'
      case 'symbol':
      case 'undefined':
        return false;
    }
  }

  return typeWant === typeGet;
}

export const dataValidators = {
  PrimaryKeyShouldExist: {
    validate(table, data) {
      const val = data[table.keyField];

      if (val === undefined || val === null) {
        throw new Error(
          `[Table(${table.name})]: Primary key field '${table.keyField}' is required but not set.`
        );
      }
    },
  },
  PrimaryKeyShouldNotBeUpdated: {
    validate(table, data) {
      if (data[table.keyField] !== undefined) {
        throw new Error(
          `[Table(${table.name})]: Primary key field '${table.keyField}' can't be updated.`
        );
      }
    },
  },
  DataTypeShouldMatch: {
    validate(table, data) {
      for (const key in data) {
        const field = table.schema[key];

View on GitHub (pinned to b4c8548c09)