Flask-SQLAlchemy can't find tables after upgrading from 2.5 to 3.x
Asked Answered
H

1

0

I'm upgrading the required packages for my Flask app and have run into a problem. I bumped up from Flask 1.1 to 2.2 and am now trying to bump Flask-SQLAlchemy up from 2.5 to 3.x. I upgraded with pip and the app starts, but when I request a page, SQLAlchemy complains that it can't find any of my tables.

I'm using SQLite locally and I can see the tables using the SQLite command line tool, so I know they exist / didn't get clobbered somehow. I assume this is just a communication issue.

Heptameter answered 19/3, 2023 at 20:50 Comment(0)
H
0

Since the tables still exist, I started looking in the Flask-SQLAlchemy 3.0.0 change log, and I found this:

Configuring SQLite with a relative path is relative to app.instance_path instead of app.root_path. The instance folder is created if necessary.

My old db existed in ./my_flask_app/site.db and my config was set as "SQLALCHEMY_DATABASE_URI": "sqlite:///site.db". Flask-SQLAlchemy is now looking for it in ./instance/site.db. It had even helpfully created an empty site.db file there for me. I deleted the auto-generated one and moved the proper file into that home and everything works again.

Turns out reading the change log is helpful. Who would've thought.

Heptameter answered 19/3, 2023 at 20:50 Comment(1)
If you do this, don't forget to exclude the instance folder from your version control, as recommended by the Flask docs: "The instance folder is designed to not be under version control and be deployment specific. It’s the perfect place to drop things that either change at runtime or configuration files."Heptameter

© 2022 - 2024 — McMap. All rights reserved.