{"record":{"id":"7790edbd9aa9368f","repo":"pentaho/pentaho-kettle","slug":"elemwithkey-is-already-included","errorCode":null,"errorMessage":"${elemWithKey} is already included.","messagePattern":"(.+?) is already included\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"plugins/core-ui/src/main/resources/app/pentaho/lang/Collection.js","lineNumber":118,"sourceCode":"\n    _getElemKey: function(elem) {\n      return elem.key;\n    },\n\n    _sayElemWithKey: function(key) {\n      return \"A \" + this._getElemName() + \" with \" + this._getKeyName() + \" '\" + key + \"'\";\n    },\n\n    _sayElemCannotHaveNullyKey: function() {\n      return \"A \" + this._getElemName() + \" cannot have a nully \" + this._getKeyName() + \" value.\";\n    },\n\n    _adding: function(elem, index, ka) {\n      var elem2 = this._cast(elem, index, ka);\n      if(elem2 !== undefined) {\n        var key = this._getElemKey(elem2);\n        if(key == null) throw new Error(this._sayElemCannotHaveNullyKey());\n        if(this.has(key)) throw new Error(this._sayElemWithKey(key) + \" is already included.\");\n      }\n\n      return elem2;\n    },\n\n    _replacing: function(elem, index, elem0, ka) {\n      var elem2 = this._cast(elem, index, ka);\n      if(elem2 !== undefined) {\n        var key = this._getElemKey(elem2);\n        if(key == null) throw new Error(this._sayElemCannotHaveNullyKey());\n      }\n\n      return elem2;\n    },\n\n    _added: function(elem) {\n      this.__keys[this._getElemKey(elem)] = elem;\n    },","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/plugins/core-ui/src/main/resources/app/pentaho/lang/Collection.js#L100-L136","documentation":"Collection._adding throws this when an element being added casts to a valid element whose key already exists in the collection. Keyed collections enforce key uniqueness, so adding a second element with an identical key is rejected at insertion time via this Error. The message includes a human-readable description of the offending element (produced by _sayElemWithKey).","triggerScenarios":"Calling add/addN/addF or collection-constructor element arrays (anything that routes through _adding) with an element whose _getElemKey value equals an existing element's key. Also triggered by replaceAt/_replacing casts when the replacement resolves to a duplicate key.","commonSituations":"Adding the same specification item, filter, or visual role twice in pentaho component/Model config; duplicate entries in a declarative array (e.g. two `select` fields with the same name); merging configs where both base and extension define the same keyed element.","solutions":["De-duplicate the input array before passing it to add/addN or the collection constructor","Check collection.has(key) with the element's key before adding","Remove the existing element first (collection.remove(key)) if replacement was intended","If the key is computed (e.g. from a name property), rename one of the colliding elements"],"exampleFix":"// before\nlist.add({name: 'x'}); // twice -> throws\n// after\nif (!list.has('x')) list.add({name: 'x'});","handlingStrategy":"validation","validationCode":"function canAdd(list, elem) {\n  var key = elem && elem.name;\n  return key != null && !list.has(key);\n}\nif (canAdd(list, elem)) list.add(elem);","typeGuard":"function hasUniqueKey(list, elem) {\n  return elem != null && elem.name != null && !list.has(elem.name);\n}","tryCatchPattern":"try {\n  list.add(elem);\n} catch (e) {\n  if (String(e.message).indexOf('is already included') >= 0) {\n    list.remove(elem.name);\n    list.add(elem);\n  } else throw e;\n}","preventionTips":["De-duplicate arrays of keyed elements before passing them to collections","Check has(key) before every add on keyed collections","Decide explicitly whether a collision means update (remove+add) or skip"],"tags":["collection","duplicate-key"],"backgroundTag":"file-already-exists","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}