{"record":{"id":"97e808b994d9a8b9","repo":"risingwavelabs/risingwave","slug":"cannot-serialize-primary-key","errorCode":null,"errorMessage":"cannot serialize primary key","messagePattern":"cannot serialize primary key","errorType":"exception","errorClass":"SinkError::Mongodb","httpStatus":null,"severity":"error","filePath":"src/connector/src/sink/mongodb.rs","lineNumber":551,"sourceCode":"\nstruct UpsertCommandBuilder {\n    coll: String,\n    updates: Array,\n    deletes: HashMap<Vec<u8>, Document>,\n}\n\nimpl UpsertCommandBuilder {\n    fn new(coll: String) -> Self {\n        Self {\n            coll,\n            updates: Array::new(),\n            deletes: HashMap::new(),\n        }\n    }\n\n    fn add_upsert(&mut self, pk: Document, row: Document) -> Result<()> {\n        let pk_data = mongodb::bson::to_vec(&pk).map_err(|err| {\n            SinkError::Mongodb(anyhow!(err).context(\"cannot serialize primary key\"))\n        })?;\n        // under same pk, if the record currently being upserted was marked for deletion previously, we should\n        // revert the deletion, otherwise, the upserting record may be accidentally deleted.\n        // see https://github.com/risingwavelabs/risingwave/pull/17102#discussion_r1630684160 for more information.\n        self.deletes.remove(&pk_data);\n\n        self.updates.push(bson!( {\n            \"q\": pk,\n            \"u\": bson!( {\n                \"$set\": row,\n            }),\n            \"upsert\": true,\n            \"multi\": false,\n        }));\n\n        Ok(())\n    }\n","sourceCodeStart":533,"sourceCodeEnd":569,"githubUrl":"https://github.com/risingwavelabs/risingwave/blob/6469eb736d691e8e9b8a419a57edd6429ca77417/src/connector/src/sink/mongodb.rs#L533-L569","documentation":"Raised in UpsertCommandBuilder::add_upsert when mongodb::bson::to_vec fails to serialize the primary key document into BSON bytes. This indicates the generated primary key document contains values unrepresentable in BSON (e.g. unsupported types), a programming/encoding bug rather than user misconfiguration.","triggerScenarios":"Thrown at src/connector/src/sink/mongodb.rs:551 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check that the primary key document contains only BSON-serializable values.","Verify the primary key schema does not contain unsupported types (e.g., NaN/Infinity floats, invalid datetime).","Fix upstream data types or the sink PK definition so the key can be serialized."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"6469eb736d691e8e9b8a419a57edd6429ca77417","analyzedAt":"2026-09-11T21:06:21.487Z","contentChangedAt":"2026-09-11T21:06:21.487Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}