{"record":{"id":"27c76c0283b42950","repo":"apache/superset","slug":"database-not-found-27c76c","errorCode":null,"errorMessage":"Database not found.","messagePattern":"Database not found\\.","errorType":"http","errorClass":"DatabaseNotFoundError","httpStatus":404,"severity":"error","filePath":"superset/commands/database/validate_sql.py","lineNumber":150,"sourceCode":"                    \"Please recheck your query.\\n\"\n                    \"Exception: %(ex)s\",\n                    validator=self._validator.name,\n                    ex=ex,\n                ),\n                error_type=SupersetErrorType.GENERIC_DB_ENGINE_ERROR,\n                level=ErrorLevel.ERROR,\n            )\n\n            # Return as a 400 if the database error message says we got a 4xx error\n            if re.search(r\"([\\W]|^)4\\d{2}([\\W]|$)\", str(ex)):\n                raise ValidatorSQL400Error(superset_error) from ex\n            raise ValidatorSQLError(superset_error) from ex\n\n    def validate(self) -> None:\n        # Validate/populate model exists\n        self._model = DatabaseDAO.find_by_id(self._model_id)\n        if not self._model:\n            raise DatabaseNotFoundError()\n\n        spec = self._model.db_engine_spec\n        validators_by_engine = app.config[\"SQL_VALIDATORS_BY_ENGINE\"]\n        if not validators_by_engine or spec.engine not in validators_by_engine:\n            raise NoValidatorConfigFoundError(\n                SupersetError(\n                    message=__(\n                        \"no SQL validator is configured for %(engine_spec)s\",\n                        engine_spec=spec.engine,\n                    ),\n                    error_type=SupersetErrorType.GENERIC_DB_ENGINE_ERROR,\n                    level=ErrorLevel.ERROR,\n                ),\n            )\n        validator_name = validators_by_engine[spec.engine]\n        self._validator = get_validator_by_name(validator_name)\n        if not self._validator:\n            raise NoValidatorFoundError(","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/apache/superset/blob/f4587218dd19d046c3e4d00063e7d27f8a2ed354/superset/commands/database/validate_sql.py#L132-L168","documentation":"DatabaseNotFoundError (HTTP 404) is raised in ValidatorSQLCommand.validate() when DatabaseDAO.find_by_id(self._model_id) returns None — the database id supplied to the validate_sql command does not correspond to an existing Database row.","triggerScenarios":"POST /api/v1/database/validate_sql with a `database_id` (or path parameter) that is deleted or never existed; the Database row being deleted between the client loading the picker and submitting the request; passing a string id that coerces to a non-matching value.","commonSituations":"Stale frontend state after an admin deletes a database connection; scripts reusing recorded database ids against a fresh metadata DB; environment mismatch (dev id referenced against prod metadata).","solutions":["Confirm the database id exists: GET /api/v1/database/<id> — if it 404s, pick a valid connection from GET /api/v1/database/.","If the database was deleted, recreate the connection and re-issue the validation with the new id.","In scripts, resolve the id by database_name at runtime instead of hardcoding it."],"exampleFix":"# before\ncurl -X POST /api/v1/database/999/validate_sql -d '{\"sql\": \"SELEC 1\"}'\n\n# after — resolve a live id first\ndb_id=$(curl -s /api/v1/database/ | jq '.result[0].id')\ncurl -X POST /api/v1/database/$db_id/validate_sql -d '{\"sql\": \"SELEC 1\"}'","handlingStrategy":"validation","validationCode":"# Confirm the database id exists before validating SQL against it\nfrom superset.databases.dao import DatabaseDAO\n\ndef database_exists(database_id: int) -> bool:\n    return DatabaseDAO.find_by_id(database_id) is not None","typeGuard":null,"tryCatchPattern":"from superset.commands.database.exceptions import DatabaseNotFoundError\ntry:\n    ValidatorSQLCommand(database_id, payload).run()\nexcept DatabaseNotFoundError:\n    # 404: re-resolve a live database id and prompt the user to reselect\n    refresh_database_picker()","preventionTips":["Resolve database ids at call time (by name) instead of caching them in scripts.","Handle 404 by re-fetching the database list so users reselect a valid connection.","When a database is deleted, flush dependent UI state (pickers, saved queries) that references its id."],"tags":["sql-validation","not-found","database","flask-api"],"backgroundTag":null,"analyzedSha":"f4587218dd19d046c3e4d00063e7d27f8a2ed354","analyzedAt":"2026-08-14T22:39:27.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}