Open gitgithan opened 1 year ago
Alembic basically works this way:
alembic_revision
in your database to keep track of the latest applied revision.So if you want to recreate an initial migration from scratch, you need to start with an empty database and delete all existing migrations.
Book says run
alembic revision --autogenerate -m "initial migration"
, implicitly saying run from the folderBuilding-Data-Science-Applications-with-FastAPI
sincealembic.ini
is referencingscript_location = chapter6/sqlalchemy_relationship/alembic
instead of the defaultalembic
.However this will result in errors saying
FAILED: No config file 'alembic.ini' found, or file has no '[alembic]' section
.What i needed to make this work was to specify the path of alembic.ini using -c option.
alembic -c chapter6/sqlalchemy_relationship/alembic.ini revision --autogenerate -m "initial migration"
, same foralembic -c chapter6/sqlalchemy_relationship/alembic.ini upgrade head
.Another issue When I first follow along the book, i get a empty migration script
I found this message (https://stackoverflow.com/a/67509934/8621823) explaining that if the table is already there, the migration script will be empty at those portions, so I deleted the sqlite database, and deleted all the migration scripts, tried again and it worked. I also had to delete the database because somewhere along the way, running
alembic revision
gave meERROR [alembic.util.messaging] Can't locate revision identified by 'ca9e1b2549f3'
. I'm guessing this revision was stored in the database, and it was looking for this revision under alembic/versions folder, which i had cleared.Question
Can't locate revision error
is right, and whether deleting sqlite database and all the existing migration scripts is necessary)alembic revision
command to add some new tables and columns underdef upgrade
in the migration script, to be applied withalembic upgrade head
to change the database?