3

I am trying to replace Alembic base revision in a Flask application. The reason is that the Alembic revisions have not been created correctly for this project and I want to make them in correct order from initial database to the latest version. To do so, I had to create an initial revision and replaced it with the current base revision in the tree. Here is what the tree looked like before any changes:

>> alembic history
20b081e106eb -> f6b6d50b4012 (head), revision 6
604059d119b3 -> 1f6f72cb12a9, revision 5
fa56f5d34a18 -> 604059d119b3, revision 4
4e8f28c411ea -> fa56f5d34a18, revision 3
8494e6010c15 -> 4e8f28c411ea, revision 2
37b8265891db -> 8494e6010c15, revision 1
<base> -> 37b8265891db, previous base

I changed the down_revision for the generated revision to None and set the down_revision for the previous base to the generated revision_ID. When I check the history, I see that everything is correctly in order and my new base revision is replaced with previous base and the previous base revises the generated base:

>> alembic history
20b081e106eb -> f6b6d50b4012 (head), revision 6
604059d119b3 -> 1f6f72cb12a9, revision 5
fa56f5d34a18 -> 604059d119b3, revision 4
4e8f28c411ea -> fa56f5d34a18, revision 3
8494e6010c15 -> 4e8f28c411ea, revision 2
37b8265891db -> 8494e6010c15, revision 1
47f0eb12e6b5 -> 37b8265891db, previous base
<base> -> 47f0eb12e6b5, initial database (new)

Now, when I run alembic upgrade head, I'm getting this error:

 File "/export/content/lid/apps/sec-assistant/dev-i001/libexec/sec-assistant_5a281323-d247-4339-afcf-79b88f9fab38/site-packages/alembic/script/base.py", line 329, in _upgrade_revs
        revs = list(revs)
      File "/export/content/lid/apps/sec-assistant/dev-i001/libexec/sec-assistant_5a281323-d247-4339-afcf-79b88f9fab38/site-packages/alembic/script/revision.py", line 652, in _iterate_revisions
        uppers = util.dedupe_tuple(self.get_revisions(upper))
      File "/export/content/lid/apps/sec-assistant/dev-i001/libexec/sec-assistant_5a281323-d247-4339-afcf-79b88f9fab38/site-packages/alembic/script/revision.py", line 300, in get_revisions
        resolved_id, branch_label = self._resolve_revision_number(id_)
      File "/export/content/lid/apps/sec-assistant/dev-i001/libexec/sec-assistant_5a281323-d247-4339-afcf-79b88f9fab38/site-packages/alembic/script/revision.py", line 433, in _resolve_revision_number
        self._revision_map
      File "/export/content/lid/apps/sec-assistant/dev-i001/libexec/sec-assistant_5a281323-d247-4339-afcf-79b88f9fab38/site-packages/alembic/util/langhelpers.py", line 240, in __get__
        obj.__dict__[self.__name__] = result = self.fget(obj)
      File "/export/content/lid/apps/sec-assistant/dev-i001/libexec/sec-assistant_5a281323-d247-4339-afcf-79b88f9fab38/site-packages/alembic/script/revision.py", line 151, in _revision_map
        down_revision = map_[downrev]
    KeyError: '47f0eb12e6b5'

Is there any way that I can fix this without deleting and regenerating all the revisions from base to head?

davidism
  • 121,510
  • 29
  • 395
  • 339
A23149577
  • 2,045
  • 2
  • 40
  • 74
  • Hi, I had a go at trying to create a simplified reproduction of this but no success. Perhaps you can have a look at https://github.com/5uper5hoot/stackoverflow-answers/tree/60793463/stackoverflow_answers/60793463, check out the readme section where I list the steps to attempt to reproduce and let me know what needs to change in order to get a MCVE going. Feel free to even PR the repo with necessary changes. – SuperShoot Mar 21 '20 at 23:10
  • Thank you for creating the repo and examining the issue thoroughly. Surprisingly, changing revision ID from `'47f0eb12e6b5'` to `"47f0eb12e6b5"` solved the problem! – A23149577 Mar 22 '20 at 19:23
  • 1
    That's interesting, [black](https://github.com/psf/black) automatically swaps out those single quotes for double quotes whenever I save a `.py` file, but should make absolutely no difference to runtime behavior, one way or the other. – SuperShoot Mar 22 '20 at 22:40
  • I had to remove the offending migration and create it again with `alembic revision ...`, no idea what was causing this :shrug: – Mauricio Scheffer Apr 21 '23 at 08:03

0 Answers0