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

In summary, the conversation centers around the issue of adding flask migrate to an existing project. The individual is using Visual Studio Code and has a database file already created. They are trying to access the environment variable SQLALCHEMY_DATABASE_URI, but are running into errors. Suggestions are made to try different combinations of installing and uninstalling packages, as well as creating a Linux VM.
  • #1
rgtr
92
8
TL;DR Summary
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
  • #2
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 bikashdaga
  • #3
Please post Python code using the </> button so we can read it more easily.
 
  • #4
From the error message it seems that you are trying to access
Python:
current_app.extensions.migrate.migrate
. Is this correct?
 
  • #5
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:
  • #6
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 jim mcnamara
  • #7
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.

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'
 
  • #8
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.
 
  • #9
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 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 ...
 

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

1. Why is my code not recognizing the Alembic library in Python Flask?

There could be several reasons for this. First, make sure you have installed the Alembic library correctly using a package manager like pip. Also, check that you have imported the library correctly in your code. Additionally, ensure that you have set up the Alembic configuration file correctly and that the necessary database connections are established.

2. How do I initialize Alembic in my Python Flask project?

To initialize Alembic in your project, you need to first create a migration directory and a configuration file. Next, run the Alembic init command, passing in the path to your configuration file. This will create a new migration script that you can use to make changes to your database schema.

3. Why am I getting errors when running Alembic commands in my Python Flask project?

This could be due to several reasons. Common errors include incorrect database connection settings, missing dependencies, or improperly formatted migration scripts. Check the error message for more information and troubleshoot accordingly.

4. How do I create a new migration using Alembic in Python Flask?

To create a new migration, run the Alembic revision command, passing in a descriptive name for your migration. This will create a new migration script in your migration directory. Next, make the necessary changes to your database schema in the migration script and run the Alembic upgrade command to apply the changes to your database.

5. How do I rollback a migration in my Python Flask project using Alembic?

To rollback a migration, first, run the Alembic history command to view a list of all executed migrations. Then, use the Alembic downgrade command, passing in the name of the migration you want to rollback to. This will revert the changes made by the specified migration. You can also use the Alembic downgrade command with the --version flag to rollback to a specific version of a migration.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
1
Views
1K
Replies
7
Views
478
  • Programming and Computer Science
Replies
5
Views
10K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
2
Replies
62
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
Back
Top