{"record":{"id":"ea9f97fef9206543","repo":"apache/superset","slug":"an-error-occurred-while-creating-the-value-ea9f97","errorCode":null,"errorMessage":"An error occurred while creating the value.","messagePattern":"An error occurred while creating the value\\.","errorType":"exception","errorClass":"KeyValueCreateFailedError","httpStatus":500,"severity":"error","filePath":"superset/daos/key_value.py","lineNumber":131,"sourceCode":"        try:\n            encoded_value = codec.encode(value)\n        except Exception as ex:\n            raise KeyValueCreateFailedError(\"Unable to encode value\") from ex\n        entry = KeyValueEntry(\n            resource=resource.value,\n            value=encoded_value,\n            created_on=datetime.now(),\n            created_by_fk=get_user_id(),\n            expires_on=expires_on,\n        )\n        if key is not None:\n            try:\n                if isinstance(key, UUID):\n                    entry.uuid = key\n                else:\n                    entry.id = key\n            except ValueError as ex:\n                raise KeyValueCreateFailedError() from ex\n        db.session.add(entry)\n        return entry\n\n    @staticmethod\n    def upsert_entry(\n        resource: KeyValueResource,\n        value: Any,\n        codec: KeyValueCodec,\n        key: Key,\n        expires_on: datetime | None = None,\n    ) -> KeyValueEntry:\n        \"\"\"\n        Update an existing entry or create it if it does not exist.\n\n        Because this overwrites any existing entry for the key (expired or not), it\n        does not require a prior :meth:`delete_expired_entries` call.\n        \"\"\"\n        if entry := KeyValueDAO.get_entry(resource, key):","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/apache/superset/blob/f4587218dd19d046c3e4d00063e7d27f8a2ed354/superset/daos/key_value.py#L113-L149","documentation":"Raised by KeyValueDAO.create_entry when an explicit key is supplied but assigning it to the entry raises ValueError. The assignment branches on type: UUID keys go to entry.uuid, anything else goes to entry.id; entry.id is an integer column, so a non-UUID, non-integer key (e.g. a random string) makes SQLAlchemy/psycopg raise ValueError, which is re-raised as KeyValueCreateFailedError ('An error occurred while creating the value.'). The message is the generic CreateFailedError default, distinct from the encode failure message.","triggerScenarios":"Calling create_entry(key='my-string-key', ...) for a resource whose key column expects int or UUID; passing an int-like string that SQLAlchemy still rejects in the column assignment path; mixing key types between resources (UUID-keyed dashboard tabs vs int-keyed entries) and reusing the wrong key object.","commonSituations":"Building a new key-value resource and generating keys with get_random_string() instead of uuid4()/ints; refactoring a resource from int keys to UUID keys while old callers still pass strings; copy-pasting the explicit-key code path from a UUID resource into an int-keyed one.","solutions":["Match the key type to the resource schema: pass a uuid.UUID (e.g. uuid.uuid4()) for UUID-keyed resources, an int for integer-keyed resources.","Do not pass arbitrary string keys; if you need namespaced keys, store them as part of the encoded value or add a dedicated column.","Inspect the exception's __cause__ to confirm the ValueError came from key assignment, then fix the key type at the call site.","When creating entries with explicit keys and an expires_on, first call KeyValueDAO.delete_expired_entries for that resource as the docstring requires, so unique-constraint collisions on the same key do not surface as a different create failure."],"exampleFix":"# before\nKeyValueDAO.create_entry(resource, value, codec, key='tab-abc123')\n# -> ValueError wrapped in KeyValueCreateFailedError\n\n# after\nimport uuid\nKeyValueDAO.create_entry(resource, value, codec, key=uuid.uuid4())  # UUID-keyed resource\nKeyValueDAO.create_entry(resource, value, codec, key=42)  # int-keyed resource","handlingStrategy":"type-guard","validationCode":"from uuid import UUID\n\ndef valid_kv_key(key) -> bool:\n    return isinstance(key, UUID) or isinstance(key, int)","typeGuard":"from uuid import UUID\n\ndef is_valid_kv_key(key: object) -> TypeGuard[UUID | int]:\n    return isinstance(key, (UUID, int))","tryCatchPattern":"from superset.key_value.commands.exceptions import KeyValueCreateFailedError\ntry:\n    KeyValueDAO.create_entry(resource, value, codec, key=key)\nexcept KeyValueCreateFailedError as err:\n    if isinstance(err.__cause__, ValueError):\n        # key type mismatch with the resource's key column\n        key = uuid.uuid4()  # or int per resource schema, then retry once","preventionTips":["Derive keys from one helper per resource so the UUID/int choice is encoded in one place.","Never use random string keys where the schema declares int or UUID columns.","When using explicit keys plus expires_on, call delete_expired_entries first as documented."],"tags":["key-value","dao","validation","uuid"],"backgroundTag":null,"analyzedSha":"f4587218dd19d046c3e4d00063e7d27f8a2ed354","analyzedAt":"2026-08-14T22:39:27.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}