Running SQLAlchemy with uvicorn: Elegant way to ensure that multiple workers do not try to call Metadata.create_all?
I am working on one a Python application which uses a FastAPI, SQLAlchemy, Pydantic/psycopg2, and Uvicorn stack. The project is structured in a way similar to the “Bigger Applications – Multiple Files” by FastAPI at fastapi[dot]tiangolo[dot]com/tutorial/bigger-applications/
, where an SQLAlchemy MetaData named Base
is created that contains all of the application’s tables as well as an SQLAlchemy Engine object named engine
. In the main function, I then call: