miguelgrinberg / microblog

The microblogging application developed in my Flask Mega-Tutorial series. This version maps to the 2024 Edition of the tutorial.
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
MIT License
4.51k stars 1.61k forks source link

Chapter 4: Issues when using 'flask db migrate' #396

Open gbmerrall opened 1 month ago

gbmerrall commented 1 month ago

Two issues occur when running 'flask db migrate'

  1. using -m flag causes a runtime error in alembic

$ flask db migrate -m "users table" usage: alembic [-h] [--version] [-c CONFIG] [-n NAME] [-x X] [--raiseerr] [-q] {branches,check,current,downgrade,edit,ensure_version,heads,history,init,list_templates,merge,revision,show,stamp,upgrade} ... alembic: error: argument {branches,check,current,downgrade,edit,ensure_version,heads,history,init,list_templates,merge,revision,show,stamp,upgrade}: invalid choice: 'users table' (choose from 'branches', 'check', 'current', 'downgrade', 'edit', 'ensure_version', 'heads', 'history', 'init', 'list_templates', 'merge', 'revision', 'show', 'stamp', 'upgrade')

  1. If I remove the -m flag I get an module not found error

$ flask db migrate

Traceback (most recent call last): File "/Users/johndoe/.local/share/virtualenvs/microblog-IrJMmESS/bin/alembic", line 8, in sys.exit(main()) ^^^^^^ File "/Users/johndoe/.local/share/virtualenvs/microblog-IrJMmESS/lib/python3.12/site-packages/alembic/config.py", line 636, in main CommandLine(prog=prog).main(argv=argv) File "/Users/johndoe/.local/share/virtualenvs/microblog-IrJMmESS/lib/python3.12/site-packages/alembic/config.py", line 626, in main self.run_cmd(cfg, options) File "/Users/johndoe/.local/share/virtualenvs/microblog-IrJMmESS/lib/python3.12/site-packages/alembic/config.py", line 603, in run_cmd fn( File "/Users/johndoe/.local/share/virtualenvs/microblog-IrJMmESS/lib/python3.12/site-packages/alembic/command.py", line 406, in upgrade script.run_env() File "/Users/johndoe/.local/share/virtualenvs/microblog-IrJMmESS/lib/python3.12/site-packages/alembic/script/base.py", line 582, in run_env util.load_python_file(self.dir, "env.py") File "/Users/johndoe/.local/share/virtualenvs/microblog-IrJMmESS/lib/python3.12/site-packages/alembic/util/pyfiles.py", line 95, in load_python_file module = load_module_py(module_id, path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/johndoe/.local/share/virtualenvs/microblog-IrJMmESS/lib/python3.12/site-packages/alembic/util/pyfiles.py", line 113, in load_module_py spec.loader.exec_module(module) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 995, in exec_module File "", line 488, in _call_with_frames_removed File "/Users/johndoe/Code/microblog/db/env.py", line 7, in from app import create_app ModuleNotFoundError: No module named 'app'

I discovered this when adapting the tutorial to my own situation so to confirm if it was me or not, I checked out tag 0.3 from the github repo and followed Chapter 4 up to running ''flask db migrate;

It's also worth pointing out that 'flask db init' also provides different output from the tutorial.

$ flask db init alembic.ini was created in the root of your project db/ was created with your Alembic configs, versions/ directory and seeds.py

Versions of things

These are slightly different to what's in requirements.txt in the repo. I can't compare versions for flask-db as it's not listed there.

miguelgrinberg commented 1 month ago

I don't know what "flask-db" is. This tutorial does not use that package. Please uninstall it, as it is probably interfering with flask-migrate.