{"record":{"id":"f7d257d0557f87fb","repo":"redis/redis-py","slug":"given-database-is-not-a-member-of-database-list","errorCode":null,"errorMessage":"Given database is not a member of database list","messagePattern":"Given database is not a member of database list","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"redis/asyncio/multidb/client.py","lineNumber":164,"sourceCode":"    def get_databases(self) -> Databases:\n        \"\"\"\n        Returns a sorted (by weight) list of all databases.\n        \"\"\"\n        return self._databases\n\n    async def set_active_database(self, database: AsyncDatabase) -> None:\n        \"\"\"\n        Promote one of the existing databases to become an active.\n        \"\"\"\n        exists = None\n\n        for existing_db, _ in self._databases:\n            if existing_db == database:\n                exists = True\n                break\n\n        if not exists:\n            raise ValueError(\"Given database is not a member of database list\")\n\n        await self._check_db_health(database)\n\n        if database.circuit.state == CBState.CLOSED:\n            highest_weighted_db, _ = self._databases.get_top_n(1)[0]\n            await self.command_executor.set_active_database(\n                database, GeoFailoverReason.MANUAL\n            )\n            return\n\n        raise NoValidDatabaseException(\n            \"Cannot set active database, database is unhealthy\"\n        )\n\n    async def add_database(\n        self, config: DatabaseConfig, skip_initial_health_check: bool = True\n    ):\n        \"\"\"","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/asyncio/multidb/client.py#L146-L182","documentation":"Raised as ValueError by MultiDBClient.set_active_database() when the Database object passed in is not present in the client's weighted database list. The method intentionally refuses to promote an unknown database because routing/health tracking is keyed off the configured set.","triggerScenarios":"Calling await client.set_active_database(db) with a Database instance that was not returned by get_databases() / not added via MultiDbConfig.databases_config or add_database(). Commonly: constructing a brand-new Database(...) by hand, passing a Database belonging to a different MultiDBClient, or passing one obtained before remove_database() was called on it.","commonSituations":"Manually rebuilding a Database object to 'force' a failover target, holding stale references after topology reconfiguration, copy/paste between two MultiDBClient instances, or unit tests that fabricate a Database instead of pulling it from get_databases().","solutions":["Always obtain the target via client.get_databases() and pass one of those entries to set_active_database.","If you need a brand-new endpoint, use await client.add_database(config) first, then promote the returned/added Database.","Before calling, guard with: if db not in {d for d, _ in client.get_databases()}: ... to fail with your own message.","Drop any cached Database references after remove_database() and re-query get_databases()."],"exampleFix":"// before\ntarget = Database(client=Redis.from_url(\"redis://other:6379\"), circuit=cb, weight=1)\nawait client.set_active_database(target)  # ValueError\n\n// after\navailable = {d for d, _ in client.get_databases()}\ntarget = next(d for d in available if d.weight == max(w for _, w in client.get_databases()))\nawait client.set_active_database(target)","handlingStrategy":"validation","validationCode":"def is_known_database(client, db) -> bool:\n    return any(existing is db for existing, _ in client.get_databases())\n\n# before promoting:\nif not is_known_database(client, target):\n    raise ValueError(f\"{target!r} is not in client.get_databases()\")","typeGuard":"from redis.asyncio.multidb.database import AsyncDatabase\n\ndef is_async_database(obj) -> bool:\n    return isinstance(obj, AsyncDatabase)","tryCatchPattern":"try:\n    await client.set_active_database(target)\nexcept ValueError as e:\n    if \"not a member of database list\" in str(e):\n        # fetch a fresh reference and retry, or surface to the operator\n        target = next(d for d, _ in client.get_databases() if d.circuit.state.name == \"CLOSED\")\n        await client.set_active_database(target)\n    else:\n        raise","preventionTips":["Always source the Database from client.get_databases(), never hand-construct it.","Invalidate cached Database references whenever remove_database() runs.","Do not share Database objects across MultiDBClient instances.","Add an integration test that asserts set_active_database accepts only known DBs."],"tags":["multidb","validation","config","failover"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}