pubkey/rxdb · error · RxError

SC39

SC39

Error message

        RxDB Error-Code: ${message}.
        Hint: Error messages are not included in RxDB core to reduce build size.
        To show the full error messages and to ensure that you do not make any mistakes when using RxDB,
        use the dev-mode plugin when you are in development mode: https://rxdb.info/dev-mode.html?console=error
        
Find out more about this error here: https://rxdb.info/errors.html?console=errors#SC39 
Still stuck? Ask in the RxDB Discord: https://rxdb.info/chat 

What it means

SC39 is thrown in the RxSchema constructor when the schema's primary key property has no maxLength set. RxDB requires string primary keys to declare a maximum length so storage backends can size keys and validate document ids. This check runs even without the dev-mode plugin because invalid schemas are such a common mistake.

Source

Thrown at src/rx-schema.ts:54

    public readonly primaryPath: StringKeys<RxDocumentData<RxDocType>>;
    public finalFields: string[];

    constructor(
        public readonly jsonSchema: RxJsonSchema<RxDocumentData<RxDocType>>,
        public readonly hashFunction: HashFunction
    ) {
        this.indexes = getIndexes(this.jsonSchema);

        // primary is always required
        this.primaryPath = getPrimaryFieldOfPrimaryKey(this.jsonSchema.primaryKey);

        /**
         * Many people accidentally put in wrong schema state
         * without the dev-mode plugin, so we need this check here
         * even in non-dev-mode.
         */
        if (!jsonSchema.properties[this.primaryPath].maxLength) {
            throw newRxError('SC39', { schema: jsonSchema });
        }

        this.finalFields = getFinalFields(this.jsonSchema);
    }

    public get version(): number {
        return this.jsonSchema.version;
    }

    public get defaultValues(): { [P in keyof RxDocType]: RxDocType[P] } {
        const values = {} as { [P in keyof RxDocType]: RxDocType[P] };
        Object
            .entries(this.jsonSchema.properties)
            .filter(([, v]) => Object.prototype.hasOwnProperty.call(v, 'default'))
            .forEach(([k, v]) => (values as any)[k] = (v as any).default);
        return overwriteGetterForCaching(
            this,
            'defaultValues',

View on GitHub (pinned to af6fb65f94)

Solutions

  1. Add maxLength to the primary key property in your schema, e.g. { id: { type: 'string', maxLength: 100 } }.
  2. Use a string primary key with maxLength: 100 as recommended in the RxDB quickstart.
  3. Validate your schema with the dev-mode plugin to catch other schema mistakes at startup.
  4. For composite primary keys, ensure the referenced key fields are string properties with maxLength set.

Example fix

// before
const schema = {
  title: 'hero',
  version: 0,
  primaryKey: 'id',
  properties: { id: { type: 'string' } }
};
// after
const schema = {
  title: 'hero',
  version: 0,
  primaryKey: 'id',
  properties: { id: { type: 'string', maxLength: 100 } }
};
Defensive patterns

Strategy: validation

Validate before calling

function primaryKeyHasMaxLength(schema: RxJsonSchema<any>): boolean {
  const primaryPath = typeof schema.primaryKey === 'string'
    ? schema.primaryKey
    : (schema.primaryKey as any).key;
  const prop = schema.properties[primaryPath];
  return !!prop && prop.type === 'string' && typeof prop.maxLength === 'number' && prop.maxLength > 0;
}

Try / catch

try {
  await db.addCollections({ heroes: { schema: mySchema } });
} catch (err: any) {
  if (err.code === 'SC39') {
    console.error('Primary key property needs maxLength, fix the schema', err.parameters?.schema);
  } else throw err;
}

Prevention

When it happens

Trigger: Calling createRxDatabase().addCollections() (or constructing RxSchema directly) with a JSON schema whose primaryKey is a string field that lacks maxLength in its property definition, e.g. { properties: { id: { type: 'string' } }, primaryKey: 'id' } with no maxLength: 100.

Common situations: Copy-pasting a schema and dropping the maxLength; switching from a numeric to a string primary key; following outdated tutorials or LLM-generated schemas that omit maxLength; upgrading RxDB where the check became stricter.

Related errors


AI-assisted analysis of pubkey/rxdb@af6fb65f94 (2026-08-31). Data as JSON: /api/errors/6bc353b248330f51. Report an issue: GitHub.