{"record":{"id":"acd71a61c02d3069","repo":"pubkey/rxdb","slug":"doc19","errorCode":"DOC19","errorMessage":"\n        RxDB Error-Code: ${message}.\n        Hint: Error messages are not included in RxDB core to reduce build size.\n        To show the full error messages and to ensure that you do not make any mistakes when using RxDB,\n        use the dev-mode plugin when you are in development mode: https://rxdb.info/dev-mode.html?console=error\n        \nFind out more about this error here: https://rxdb.info/errors.html?console=errors#DOC19 \nStill stuck? Ask in the RxDB Discord: https://rxdb.info/chat \n","messagePattern":"\n        RxDB Error-Code: \\$\\{message\\}\\.\n        Hint: Error messages are not included in RxDB core to reduce build size\\.\n        To show the full error messages and to ensure that you do not make any mistakes when using RxDB,\n        use the dev-mode plugin when you are in development mode: https://rxdb\\.info/dev-mode\\.html\\?console=error\n        \nFind out more about this error here: https://rxdb\\.info/errors\\.html\\?console=errors#DOC19 \nStill stuck\\? Ask in the RxDB Discord: https://rxdb\\.info/chat \n","errorType":"error_code","errorClass":"RxError","httpStatus":null,"severity":"error","filePath":"src/rx-schema-helper.ts","lineNumber":110,"sourceCode":"    primaryPath: keyof T,\n    jsonSchema: RxJsonSchema<T>,\n    documentData: RxDocumentData<T>\n): RxDocumentData<T> {\n    // optimization shortcut.\n    if (typeof jsonSchema.primaryKey === 'string') {\n        return documentData;\n    }\n\n    const newPrimary = getComposedPrimaryKeyOfDocumentData<T>(\n        jsonSchema,\n        documentData\n    );\n    const existingPrimary: string | undefined = documentData[primaryPath] as any;\n    if (\n        existingPrimary &&\n        existingPrimary !== newPrimary\n    ) {\n        throw newRxError(\n            'DOC19',\n            {\n                args: {\n                    documentData,\n                    existingPrimary,\n                    newPrimary,\n                },\n                schema: jsonSchema\n            });\n    }\n\n    (documentData as any)[primaryPath] = newPrimary;\n    return documentData;\n}\n\nexport function getPrimaryFieldOfPrimaryKey<RxDocType>(\n    primaryKey: PrimaryKey<RxDocType>\n): StringKeys<RxDocType> {","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/pubkey/rxdb/blob/af6fb65f94cd70558d74c23799eda11ff1c15224/src/rx-schema-helper.ts#L92-L128","documentation":"DOC19 is thrown by fillPrimaryKey() when the document data already contains a value for the primary key field, but that value differs from the primary key RxDB computed from the composed/composite primary key fields. RxDB derives the primary key deterministically from the schema's primaryKey definition, so a pre-set primary that disagrees with the derived one would break document identity. The library throws to prevent silently creating a document whose id does not match its key fields.","triggerScenarios":"Calling collection.insert() (via fillObjectDataBeforeInsert) or collection.bulkInsert() with document data where data[primaryPath] is already set to a string that does not equal the value computed by joining the composite key fields with the separator, e.g. inserting {id: 'foo', firstName: 'bar', lastName: 'baz'} when the composed key yields 'bar|baz'.","commonSituations":"Migrating code from a schema with a plain primary key to a composite primary key while still setting the old id manually; copying documents between collections with different key definitions; manually crafting documents for seed/fixup scripts; deserializing stored data where the key fields were edited but the primary was not recomputed.","solutions":["Do not set the primary key field yourself; omit it and let RxDB compose it from the key fields.","If you must set it, make sure it exactly equals the composed value (fields joined by the schema's separator), e.g. use getComposedPrimaryKeyOfDocumentData() to compute it.","Check your schema's primaryKey definition (fields and separator) matches what your code assumes.","Enable the dev-mode plugin during development to see the full error message with the offending documentData, existingPrimary and newPrimary values."],"exampleFix":"// before\ncollection.insert({ id: 'foo', firstName: 'bar', lastName: 'baz' });\n// after\n// omit the primary, RxDB composes it (with separator '|')\ncollection.insert({ firstName: 'bar', lastName: 'baz' });\n// or compute it correctly:\nconst id = getComposedPrimaryKeyOfDocumentData(schema, { firstName: 'bar', lastName: 'baz' });\ncollection.insert({ id, firstName: 'bar', lastName: 'baz' });","handlingStrategy":"validation","validationCode":"import { getComposedPrimaryKeyOfDocumentData } from 'rxdb/plugins/rx-schema-helper';\nfunction validatePrimaryKey(schema: RxJsonSchema<any>, data: Record<string, any>): boolean {\n  const primaryPath = schema.primaryKey as string;\n  if (data[primaryPath] === undefined) return true; // RxDB will fill it\n  const composed = getComposedPrimaryKeyOfDocumentData(schema, data);\n  return data[primaryPath] === composed;\n}","typeGuard":"function hasConsistentPrimaryKey<T>(schema: RxJsonSchema<T>, data: T): data is T & { [k: string]: string } {\n  const primaryPath = getPrimaryFieldOfPrimaryKey(schema.primaryKey) as keyof T;\n  const existing = (data as any)[primaryPath];\n  return existing === undefined || existing === getComposedPrimaryKeyOfDocumentData(schema, data);\n}","tryCatchPattern":"try {\n  await collection.insert(data);\n} catch (err: any) {\n  if (err.code === 'DOC19') {\n    // recompute or strip the primary and retry\n    delete data[getPrimaryFieldOfPrimaryKey(collection.schema.primaryKey)];\n    await collection.insert(data);\n  } else throw err;\n}","preventionTips":["Never set the primary key manually when using a composite primary key schema.","Compute ids with getComposedPrimaryKeyOfDocumentData() when you need to know the id beforehand.","Run the dev-mode plugin in development to get full error args.","Add a unit test asserting data[primaryPath] === composed key for seed data."],"tags":["schema","primary-key","insert","validation"],"backgroundTag":"primary-key-mismatch","analyzedSha":"af6fb65f94cd70558d74c23799eda11ff1c15224","analyzedAt":"2026-08-31T23:32:37.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}