I'm self documenting a solution to db.create_all
for anyone that has this problem. Hopefully it will help someone.
Here is a list of other threads that did not help me:
- db.create_all() doesn't create tables defined in separate file
- Flask SQLAlchemy db.create_all() not creating database
- Flask-SQLAlchemy create_all doesn't work
- Flask_SQLAlchemy create_all() doesn't work
I tried creating and destroying a db
in a conftest.py
for pytest. Alembic autogenerate was working for me but for some reason db.create_all
was not. Importing the models before the db.create_all
did nothing. Neither did importing the models just after db = SQLAlchemy()
in my __init__.py
(I was using the application factory pattern). I was also using a Declarative Base, which may be part of the problem.
Every run of pytest resulted in the following error:
ERROR tests/test_main.py::test_index - sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "rule" does not exist