{"record":{"id":"e91ebb4402fcd0d4","repo":"apache/superset","slug":"the-database-was-not-found","errorCode":null,"errorMessage":"The database was not found.","messagePattern":"The database was not found\\.","errorType":"http","errorClass":"SupersetErrorException","httpStatus":404,"severity":"error","filePath":"superset/commands/dataset/duplicate.py","lineNumber":70,"sourceCode":"    @transaction(on_error=partial(on_error, reraise=DatasetDuplicateFailedError))\n    def run(self) -> Model:\n        self.validate()\n        # Declare the high-level avenue before the duplicate touches\n        # the session. The change-record listener stamps\n        # ``version_transaction.action_kind = 'clone'`` so the new\n        # dataset's baseline records read as a clone in the timeline.\n        # Method-scoped import — defers the versioning bootstrap path\n        # out of this command's module-load graph; see ``changes.py``\n        # module docstring for the broader init-order rationale.\n        from superset.versioning.changes import ACTION_KIND_CLONE, ACTION_KIND_KEY\n\n        db.session.info[ACTION_KIND_KEY] = ACTION_KIND_CLONE\n        database_id = self._base_model.database_id\n        table_name = self._properties[\"table_name\"]\n        editors = self._properties[\"editors\"]\n        database = db.session.get(Database, database_id)\n        if not database:\n            raise SupersetErrorException(\n                SupersetError(\n                    message=__(\"The database was not found.\"),\n                    error_type=SupersetErrorType.DATABASE_NOT_FOUND_ERROR,\n                    level=ErrorLevel.ERROR,\n                ),\n                status=404,\n            )\n        table = SqlaTable()\n        table.override(self._base_model)\n        table.table_name = table_name\n        table.editors = editors\n        table.database = database\n        table.is_sqllab_view = True\n        if table.sql:\n            table.sql = table.sql.strip().strip(\";\")\n        db.session.add(table)\n\n        columns = [","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/apache/superset/blob/f4587218dd19d046c3e4d00063e7d27f8a2ed354/superset/commands/dataset/duplicate.py#L52-L88","documentation":"SupersetErrorException with DATABASE_NOT_FOUND_ERROR (HTTP 404, 'The database was not found.') is raised inside the dataset duplicate command's run() when db.session.get(Database, database_id) returns None — the base dataset's stored database_id no longer matches any Database row. The base dataset itself passed validation, so this indicates its connection was deleted (or the metadata is inconsistent) between validate() and run().","triggerScenarios":"POST /api/v1/dataset/duplicate with base_model_id whose dataset references a deleted database connection; the database row removed concurrently while the duplicate request executes; metadata restored from a backup where datasets outlived their databases.","commonSituations":"Admin deletes a database connection without cascading cleanup of its datasets; environment migrations that copy datasets but not databases; race between database deletion and an in-flight duplicate.","solutions":["Check the base dataset's database reference (GET /api/v1/dataset/<base_model_id> shows database id) and verify GET /api/v1/database/<id> resolves.","If the database was deleted, recreate the connection (same engine/URI) so the orphaned dataset becomes usable, then retry the duplicate.","Clean up or re-point orphaned datasets before duplicating them; retrying immediately will keep failing with 404."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"# Ensure the base dataset's database still exists before duplicating\nfrom superset.models.core import Database\nfrom superset import db\n\ndef base_database_exists(base_model) -> bool:\n    return (\n        base_model is not None\n        and db.session.get(Database, base_model.database_id) is not None\n    )","typeGuard":null,"tryCatchPattern":"from superset.errors import SupersetErrorException\ntry:\n    DuplicateDatasetCommand(base_id, properties).run()\nexcept SupersetErrorException as ex:\n    if ex.error.error_type.name == \"DATABASE_NOT_FOUND_ERROR\":\n        # orphaned base dataset: prompt admin to recreate the connection\n        prompt_recreate_database(base_dataset.database_name)\n    else:\n        raise","preventionTips":["When deleting a database connection, clean up or export its datasets in the same operation.","Periodically scan for datasets whose database_id has no Database row (orphan report).","After metadata-DB restores, verify referential integrity of dataset->database links."],"tags":["dataset","duplicate","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"}