I am trying to use alembic in python flask and it is not working

  • Context: Python 
  • Thread starter Thread starter rgtr
  • Start date Start date
  • Tags Tags
    flask Python
Click For Summary

Discussion Overview

The discussion revolves around issues encountered while using Alembic with Flask-Migrate in a Python Flask application. Participants explore installation problems, error messages, and differences in documentation regarding database migration setup.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant mentions using Visual Studio Code with a SQLite database and describes an error encountered when trying to run migration commands.
  • Another suggests that the order of module installation might be important and proposes trying to uninstall and reinstall packages in a different order.
  • There is a request for clearer formatting of Python code to facilitate understanding of the issues being faced.
  • A participant points out a specific error related to accessing the 'migrate' extension and confirms that they are indeed trying to access it correctly.
  • One participant expresses frustration with using pip on Windows, suggesting a preference for Linux for more serious development tasks.
  • Concerns are raised about compatibility issues with Python 3.10, with a suggestion to revert to an earlier version like 3.6.
  • A solution is proposed involving adding a line of code to the application's initialization file to resolve a KeyError related to 'migrate'.
  • There is a discussion about discrepancies between different tutorials and official documentation regarding the initialization of the Migrate object, with participants noting that both methods achieve similar results.

Areas of Agreement / Disagreement

Participants express differing opinions on the best practices for installation and setup, and there is no consensus on the most effective approach to resolve the issues being faced. The discussion remains unresolved regarding the best method to handle the migration setup.

Contextual Notes

Participants mention various error messages and installation issues without fully resolving the underlying problems. There are also references to different coding practices and documentation that may lead to confusion.

rgtr
Messages
90
Reaction score
8
TL;DR
I am trying to use alembic in flask. I am following Miguel Grinberg's tutorial but I am getting an error. More details below
https://blog.miguelgrinberg.com/post/how-to-add-flask-migrate-to-an-existing-project

I am using Visual Studio Code and have a database file already created called test.db. My environment variables for test.db name is SQLALCHEMY_DATABASE_URI and the value is sqlite:///test.db In the actual code to the access the environment variable I use SQLALCHEMY_DATABASE_URI = os.environ['SQLALCHEMY_DATABASE_URI']

This website is only running on my local machine if that makes a difference. When I type
pip install flask-migrate flask db migrate.

Here is the current error



The link below shows the previous error. The odd part is that the error below was showing up I already have Flask-Login installed. I also got a similar error with Flask-Mail and Flask-WTF but I already pip installed them and the code was working. So IOW I had to pip install some things twice even though the code was working to avoid errors while running in the terminal pip install flask-migrate and flask db migrate.



Also does my current database have to be up to date?How do I fix this?

Just one thing I need to add I am using a setup similar to this.

https://github.com/CoreyMSchafer/code_snippets/tree/master/Python/Flask_Blog/11-Blueprints

Thanks
 
Technology news on Phys.org
Maybe the order of module install is important, or try rebooting your machine to see if that sorts things out once you start your app again.

Otherwise you might need to uninstall both and install in reverse order alembic then flask or flask then alembic.

Personally, I tend to use anaconda over pip as it bring a lot of preinstalled modules with it at the ost of a larger disk footprint. I've not used alembic before only flask and without using the flask-login feature.
 
  • Like
Likes   Reactions: bikashdaga
Please post Python code using the </> button so we can read it more easily.
 
From the error message it seems that you are trying to access
Python:
current_app.extensions.migrate.migrate
. Is this correct?
 
Correct.
Sorry the reason I didn't post the python code in code blocks because it was so little code and just error messages.

Also I restarted my machine many times and it didn't make a difference
 
Last edited:
The instructions say you should use
Bash:
pip install Flask-Migrate
Why are you using
Bash:
pip install flask-migrate
? Note that Python is case sensitive but Windows is not.

also have you done
Bash:
flask db init
and
Bash:
flask db migrate -m "Initial migration."
?
 
  • Like
Likes   Reactions: jim mcnamara
Sorry this was a careIess question on my part. I tried exactly what you are saying and am getting this error during flask db init.

[CODE title="error message"]Traceback (most recent call last):
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\Scripts\flask.exe\__main__.py", line 7, in <module>
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 995, in main
cli.main(args=sys.argv[1:])
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 601, in main
return super().main(*args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1053, in main
rv = self.invoke(ctx)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1659, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1659, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1395, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 754, in invoke
return __callback(*args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\decorators.py", line 26, in new_func
return f(get_current_context(), *args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\cli.py", line 445, in decorator
return __ctx.invoke(f, *args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 754, in invoke
return __callback(*args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_migrate\cli.py", line 45, in init
_init(directory, multidb, template, package)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_migrate\__init__.py", line 98, in wrapped
f(*args, **kwargs)
File "C:\Users\nmyle\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_migrate\__init__.py", line 122, in init
directory = current_app.extensions['migrate'].directory
KeyError: 'migrate'[/CODE]
 
I'm afraid I have found working with pip on Windows creates some problems which are very hard to pin down. My solution has been to only do fairly trivial stuff with Python on Windows using Jupyter notebooks. Anything more serious I do on Linux, either as a native OS or in a VM - and I particularly wouldn't want to do anything persistent like run a flask server on Windows.

So I only have two suggestions to offer:
Keep trying random combinations of installing and uninstalling packages and hope something works.
Install VirtualBox and create a Linux VM - I have had most success with Linux Mint XFCE edition.
 
I noticed in your errors that you’re using python 3.10? If so then maybe drop back to 3.6 or something as the packages you want may not support it yet.
 
  • #10
Keyerror: Migrate
Seems to be this:
add
https://lifesaver.codes/answer/flask-db-init-fails-keyerror-migrate-196

Solution:​

Adding migrate = Migrate(app, db) into __init__.py resolved the issue. (I didn't realize this was necessary for the cli functionality and database initialization, assumed it was related only to database migrations afterwards.)
 
  • Like
Likes   Reactions: rgtr
  • #11
Thanks I will try this.
 
  • #12
willem2 said:
Keyerror: Migrate
Seems to be this:
add
https://lifesaver.codes/answer/flask-db-init-fails-keyerror-migrate-196

Solution:​

Adding migrate = Migrate(app, db) into __init__.py resolved the issue. (I didn't realize this was necessary for the cli functionality and database initialization, assumed it was related only to database migrations afterwards.)
Why are the instructions different between the official flask documentations and Miguel's tutorial? Is it because of the age of the tutorial?

https://flask-migrate.readthedocs.io/en/latest/

https://blog.miguelgrinberg.com/post/how-to-add-flask-migrate-to-an-existing-project
 
  • #13
rgtr said:
Why are the instructions different between the official flask documentations and Miguel's tutorial? Is it because of the age of the tutorial?
No, its because they use two different alternatives of achieving the same result.

https://flask-migrate.readthedocs.io/en/latest/
Python:
migrate = Migrate(app, db)

https://blog.miguelgrinberg.com/post/how-to-add-flask-migrate-to-an-existing-project
Python:
migrate = Migrate()
migrate.init_app(app, db)

Actually the Grinberg API is also shown in the docs at
https://flask-migrate.readthedocs.io/en/latest/#command-reference
Python:
migrate = Migrate()
migrate.init_app(app, db)
 
  • #14
pbuk said:
No, its because they use two different alternatives of achieving the same result.

https://flask-migrate.readthedocs.io/en/latest/
Python:
migrate = Migrate(app, db)

https://blog.miguelgrinberg.com/post/how-to-add-flask-migrate-to-an-existing-project
Python:
migrate = Migrate()
migrate.init_app(app, db)

Actually the Grinberg API is also shown in the docs at
https://flask-migrate.readthedocs.io/en/latest/#command-reference
Python:
migrate = Migrate()
migrate.init_app(app, db)
Sorry I meant
Code:
flask db init

flask db migrate -m "Initial migration."

flask db upgrade


vs Miguel's ...
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
9K
Replies
3
Views
2K
Replies
7
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
15K
Replies
64
Views
10K
Replies
55
Views
7K