{"record":{"id":"4908591f89126c58","repo":"apache/cassandra","slug":"partition-key-of-additional-mutation-does-not-matc","errorCode":null,"errorMessage":"Partition key of additional mutation does not match primary update key","messagePattern":"Partition key of additional mutation does not match primary update key","errorType":"exception","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/triggers/TriggerExecutor.java","lineNumber":216,"sourceCode":"        }\n\n        ArrayList<PartitionUpdate> updates = new ArrayList<>(tmutations.size());\n        for (Mutation mutation : tmutations)\n        {\n            for (PartitionUpdate update : mutation.getPartitionUpdates())\n            {\n                validateSamePartition(tableId, key, update);\n                updates.add(update);\n            }\n        }\n        return updates;\n    }\n\n    private void validateSamePartition(TableId tableId, DecoratedKey key, PartitionUpdate update)\n    throws InvalidRequestException\n    {\n        if (!key.equals(update.partitionKey()))\n            throw new InvalidRequestException(\"Partition key of additional mutation does not match primary update key\");\n\n        if (!tableId.equals(update.metadata().id))\n            throw new InvalidRequestException(\"table of additional mutation does not match primary update table\");\n    }\n\n    private void validate(Collection<Mutation> tmutations) throws InvalidRequestException\n    {\n        for (Mutation mutation : tmutations)\n        {\n            QueryProcessor.validateKey(mutation.key().getKey());\n            for (PartitionUpdate update : mutation.getPartitionUpdates())\n                update.validate();\n        }\n    }\n\n    /**\n     * Switch class loader before using the triggers for the column family, if\n     * not loaded them with the custom class loader.","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/triggers/TriggerExecutor.java#L198-L234","documentation":"For single-partition requests augmented by triggers, every PartitionUpdate generated by the trigger must target the same partition key as the primary update. validateSamePartition throws this InvalidRequestException when the trigger's update partition key differs from the original statement's key, since mixing partitions would break the atomicity guarantees of a single-partition write.","triggerScenarios":"A trigger returns an augmented Mutation whose PartitionUpdate.partitionKey() does not equal the DecoratedKey of the row being modified in the original statement.","commonSituations":"Triggers computing derived keys (e.g. writing to an aggregate/index table under a different key); buggy trigger logic hashing or serializing the key differently; triggers designed for multi-partition batch usage attached to single-row statements.","solutions":["Fix the trigger to emit updates only for the same partition key as the incoming mutation","Move the cross-partition write out of the trigger into application logic","If cross-partition writes are required, execute them via a separate statement/batch instead of the trigger"],"exampleFix":"// before (trigger code)\nPartitionUpdate other = PartitionUpdate.builder(otherTable, differentKey).build();\n// after\nPartitionUpdate same = PartitionUpdate.builder(otherTable, sameKeyAsPrimary).build();","handlingStrategy":"validation","validationCode":"// inside trigger: assert key equality before returning\nassert update.partitionKey().equals(primaryKey) : 'trigger update key mismatch';","typeGuard":null,"tryCatchPattern":"catch InvalidRequestException mentioning 'Partition key of additional mutation' and disable the trigger","preventionTips":["Derive trigger update keys from the statement's key, never independently","Test triggers with real statement keys","Avoid computing decorated keys manually in trigger code"],"tags":["triggers","partition-key","invalid-request","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}