Search code examples
flaskflask-sqlalchemyupgrade

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


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.


Solution

  • 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.