{"id":"251d422061481b85","repo":"sqlalchemy/alembic","slug":"revision-s-not-in-map","errorCode":null,"errorMessage":"revision %s not in map","messagePattern":"revision (.+?) not in map","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"alembic/script/revision.py","lineNumber":434,"sourceCode":"                    normalized_resolved\n                )\n            else:\n                revision._normalized_resolved_dependencies = ()\n\n    def add_revision(self, revision: Revision, _replace: bool = False) -> None:\n        \"\"\"add a single revision to an existing map.\n\n        This method is for single-revision use cases, it's not\n        appropriate for fully populating an entire revision map.\n\n        \"\"\"\n        map_ = self._revision_map\n        if not _replace and revision.revision in map_:\n            util.warn(\n                \"Revision %s is present more than once\" % revision.revision\n            )\n        elif _replace and revision.revision not in map_:\n            raise Exception(\"revision %s not in map\" % revision.revision)\n\n        map_[revision.revision] = revision\n\n        revisions = [revision]\n        self._add_branches(revisions, map_)\n        self._map_branch_labels(revisions, map_)\n        self._add_depends_on(revisions, map_)\n\n        if revision.is_base:\n            self.bases += (revision.revision,)\n        if revision._is_real_base:\n            self._real_bases += (revision.revision,)\n\n        for downrev in revision._all_down_revisions:\n            if downrev not in map_:\n                util.warn(\n                    \"Revision %s referenced from %s is not present\"\n                    % (downrev, revision)","sourceCodeStart":416,"sourceCodeEnd":452,"githubUrl":"https://github.com/sqlalchemy/alembic/blob/44fb3450330204b222ff05135e1fbbbdb28c44db/alembic/script/revision.py#L416-L452","documentation":"Raised by add_revision() when called with the private _replace=True flag on a revision id that is not already present in the revision map. add_revision(_replace=True) is the internal 'replace in place' path; it assumes the revision was previously registered, so a missing entry is a programmer/contract error rather than a user-data error. Unlike most errors here it is a bare Exception (not RevisionError).","triggerScenarios":"Calling RevisionMap.add_revision(revision, _replace=True) when revision.revision is not a key in the internal _revision_map (the first branch of the if/elif at line 429-434). The non-_replace path only warns on duplicates; only _replace=True raises.","commonSituations":"Custom ScriptDirectory subclasses or plugins that mutate revisions in place; test harnesses that build a RevisionMap and try to replace a revision that was never added; code that copies a revision map into a fresh map object and forgets to add the originals first.","solutions":["Do not call add_revision with _replace=True for revisions you have not yet added; add them normally first (default _replace=False).","Verify membership before replacing: guard with 'if rev.revision in revision_map._revision_map' before calling add_revision(rev, _replace=True).","Treat this as an internal API: add_revision is documented as for single-revision use cases; prefer rebuilding the map from the script directory generator for bulk changes."],"exampleFix":"// before\nrev_map.add_revision(new_rev, _replace=True)  # raises if not present\n// after\nif new_rev.revision in rev_map._revision_map:\n    rev_map.add_revision(new_rev, _replace=True)\nelse:\n    rev_map.add_revision(new_rev)  # fresh insert","handlingStrategy":"validation","validationCode":"# before calling add_revision with _replace=True\nif revision.revision not in rev_map._revision_map:\n    rev_map.add_revision(revision)            # plain insert\nelse:\n    rev_map.add_revision(revision, _replace=True)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat add_revision as internal API; prefer rebuilding the map via the script generator.","Never assume a revision is present; check membership before _replace=True.","Avoid mutating Revision objects in place after insertion."],"tags":["revision-map","internal-api","alembic"],"analyzedSha":"44fb3450330204b222ff05135e1fbbbdb28c44db","analyzedAt":"2026-08-04T19:57:10.248Z","schemaVersion":2}