{"record":{"id":"d27dff5d1f06dc62","repo":"apache/superset","slug":"the-database-referenced-in-this-query-was-not-foun","errorCode":null,"errorMessage":"The database referenced in this query was not found. Please contact an administrator for further assistance or try again.","messagePattern":"The database referenced in this query was not found\\. Please contact an administrator for further assistance or try again\\.","errorType":"exception","errorClass":"SupersetGenericErrorException","httpStatus":500,"severity":"error","filePath":"superset/commands/sql_lab/execute.py","lineNumber":173,"sourceCode":"            self._query_dao.update(\n                query, {\"limit\": self._execution_context.query.limit}\n            )\n            return self._sql_json_executor.execute(\n                self._execution_context, rendered_query, self._log_params\n            )\n        except Exception:\n            self._query_dao.update(query, {\"status\": QueryStatus.FAILED})\n            raise\n\n    def _get_the_query_db(self) -> Database:\n        mydb: Any = self._database_dao.find_by_id(self._execution_context.database_id)\n        self._validate_query_db(mydb)\n        return mydb\n\n    @classmethod\n    def _validate_query_db(cls, database: Database | None) -> None:\n        if not database:\n            raise SupersetGenericErrorException(\n                __(\n                    \"The database referenced in this query was not found. Please \"\n                    \"contact an administrator for further assistance or try again.\"\n                )\n            )\n\n    def _save_new_query(self, query: Query) -> None:\n        \"\"\"\n        Saves the new SQL Lab query.\n\n        Committing within a transaction violates the \"unit of work\" construct, but is\n        necessary for async querying. The Celery task is defined within the confines\n        of another command and needs to read a previously committed state given the\n        `READ COMMITTED` isolation level.\n\n        To mitigate said issue, ideally there would be a command to prepare said query\n        and another to execute it, either in a sync or async manner.\n","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/apache/superset/blob/f4587218dd19d046c3e4d00063e7d27f8a2ed354/superset/commands/sql_lab/execute.py#L155-L191","documentation":"Raised by ExecuteSqlCommand._validate_query_db (superset/commands/sql_lab/execute.py:173) as SupersetGenericErrorException when DatabaseDAO.find_by_id(self._execution_context.database_id) returns None before executing a SQL Lab query. The execution payload references a database id that no longer resolves to a row; execution is aborted. Because it is the generic-error type, clients see a generic 500-style error rather than a 404.","triggerScenarios":"Executing a saved or in-flight SQL Lab query whose database_id points to a deleted database; a stale client_id/async result referencing an old database; payload constructed manually with a wrong database_id.","commonSituations":"The database connection was removed by an admin while users had open SQL Lab tabs or scheduled queries; restoring query history into an environment where database ids differ.","solutions":["Re-select an existing database in SQL Lab and re-run the query","Verify the database still exists and is visible to the user via GET /api/v1/database/{database_id}","For saved/scheduled queries, update their database reference to a live connection after database removals"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"from superset.daos.database import DatabaseDAO\n\nif DatabaseDAO.find_by_id(execution_context.database_id) is None:\n    raise ValueError(\"database_id no longer resolves; re-select a connection\")","typeGuard":null,"tryCatchPattern":"try:\n    ExecuteSqlCommand(ctx).run()\nexcept SupersetGenericErrorException as ex:\n    if \"database referenced in this query was not found\" in str(ex):\n        # prompt the user to pick a live database connection\n        ","preventionTips":["After admins delete databases, update saved queries and scheduled jobs referencing them","Validate database_id freshness before replaying saved query payloads"],"tags":["sql-lab","database","not-found","query-execution"],"backgroundTag":null,"analyzedSha":"f4587218dd19d046c3e4d00063e7d27f8a2ed354","analyzedAt":"2026-08-14T22:39:27.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}