apache/superset · error · SupersetGenericErrorException

The database referenced in this query was not found. Please

Error message

The database referenced in this query was not found. Please contact an administrator for further assistance or try again.

What it means

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.

Source

Thrown at superset/commands/sql_lab/execute.py:173

            self._query_dao.update(
                query, {"limit": self._execution_context.query.limit}
            )
            return self._sql_json_executor.execute(
                self._execution_context, rendered_query, self._log_params
            )
        except Exception:
            self._query_dao.update(query, {"status": QueryStatus.FAILED})
            raise

    def _get_the_query_db(self) -> Database:
        mydb: Any = self._database_dao.find_by_id(self._execution_context.database_id)
        self._validate_query_db(mydb)
        return mydb

    @classmethod
    def _validate_query_db(cls, database: Database | None) -> None:
        if not database:
            raise SupersetGenericErrorException(
                __(
                    "The database referenced in this query was not found. Please "
                    "contact an administrator for further assistance or try again."
                )
            )

    def _save_new_query(self, query: Query) -> None:
        """
        Saves the new SQL Lab query.

        Committing within a transaction violates the "unit of work" construct, but is
        necessary for async querying. The Celery task is defined within the confines
        of another command and needs to read a previously committed state given the
        `READ COMMITTED` isolation level.

        To mitigate said issue, ideally there would be a command to prepare said query
        and another to execute it, either in a sync or async manner.

View on GitHub (pinned to f4587218dd)

Solutions

  1. Re-select an existing database in SQL Lab and re-run the query
  2. Verify the database still exists and is visible to the user via GET /api/v1/database/{database_id}
  3. For saved/scheduled queries, update their database reference to a live connection after database removals
Defensive patterns

Strategy: validation

Validate before calling

from superset.daos.database import DatabaseDAO

if DatabaseDAO.find_by_id(execution_context.database_id) is None:
    raise ValueError("database_id no longer resolves; re-select a connection")

Try / catch

try:
    ExecuteSqlCommand(ctx).run()
except SupersetGenericErrorException as ex:
    if "database referenced in this query was not found" in str(ex):
        # prompt the user to pick a live database connection
        

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of apache/superset@f4587218dd (2026-08-14). Data as JSON: /api/errors/d27dff5d1f06dc62. Report an issue: GitHub.