{"record":{"id":"6b668fb725b6190f","repo":"apache/superset","slug":"database-not-found-6b668f","errorCode":null,"errorMessage":"Database not found.","messagePattern":"Database not found\\.","errorType":"exception","errorClass":"DatabaseNotFoundError","httpStatus":404,"severity":"error","filePath":"superset/commands/database/sync_permissions.py","lineNumber":83,"sourceCode":"        username: str | None,\n        old_db_connection_name: str | None = None,\n        db_connection: Database | None = None,\n    ):\n        \"\"\"\n        Constructor method.\n        \"\"\"\n        self.db_connection_id = model_id\n        self.username = username\n        self._old_db_connection_name: str | None = old_db_connection_name\n        self._db_connection: Database | None = db_connection\n        self._user_id: int | None = None\n\n        self.async_mode: bool = app.config[\"SYNC_DB_PERMISSIONS_IN_ASYNC_MODE\"]\n\n    @property\n    def db_connection(self) -> Database:\n        if not self._db_connection:\n            raise DatabaseNotFoundError()\n        return self._db_connection\n\n    @property\n    def old_db_connection_name(self) -> str:\n        return (\n            self._old_db_connection_name\n            if self._old_db_connection_name is not None\n            else self.db_connection.database_name\n        )\n\n    def validate(self) -> None:\n        self._db_connection = (\n            self._db_connection\n            if self._db_connection\n            else DatabaseDAO.find_by_id(self.db_connection_id)\n        )\n        if not self._db_connection:\n            raise DatabaseNotFoundError()","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/apache/superset/blob/f4587218dd19d046c3e4d00063e7d27f8a2ed354/superset/commands/database/sync_permissions.py#L65-L101","documentation":"DatabaseNotFoundError is raised by the SyncPermissionsCommand.db_connection property when the internal _db_connection reference is still None. The command lazily resolves the Database model either from a passed-in instance or from db_connection_id in validate(); accessing db_connection before a successful resolution trips this guard.","triggerScenarios":"Constructing SyncPermissionsCommand without a db_connection kwarg and reading command.db_connection (directly or via old_db_connection_name) before calling validate(); or calling it after validate() failed to resolve the id.","commonSituations":"Custom code or tests that instantiate the command and poke properties out of order; race where the database row was deleted between construction and property access.","solutions":["Call command.validate() (or run()) before touching command.db_connection or old_db_connection_name","Verify the database id exists first: DatabaseDAO.find_by_id(model_id) returns a row","If invoking from the API layer, ensure the Database connection REST endpoint passes an existing model id"],"exampleFix":"// before\n cmd = SyncPermissionsCommand(db_id, username)\n name = cmd.old_db_connection_name  # may raise DatabaseNotFoundError\n\n// after\n cmd = SyncPermissionsCommand(db_id, username)\n cmd.validate()  # resolves _db_connection or raises early\n name = cmd.old_db_connection_name","handlingStrategy":"validation","validationCode":"from superset.databases.dao import DatabaseDAO\n\nmodel = DatabaseDAO.find_by_id(db_id)\nif model is None:\n    raise LookupError(f\"no database {db_id}\")\ncmd = SyncPermissionsCommand(db_id, username, db_connection=model)\ncmd.validate()  # populates _db_connection before property access","typeGuard":null,"tryCatchPattern":"try:\n    cmd.validate()\nexcept DatabaseNotFoundError:\n    # handle missing connection\n    ...","preventionTips":["Always call validate()/run() before reading command properties","Pass a pre-fetched Database object when you have one"],"tags":["database","validation","permissions"],"backgroundTag":null,"analyzedSha":"f4587218dd19d046c3e4d00063e7d27f8a2ed354","analyzedAt":"2026-08-14T22:39:27.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}