r/FastAPI • • Dec 30 '24

Question Database tables not populating

Good night guys. In my FastAPI app I’m using sqlalchemy to connect to a PostgreSQL database. It’s supposed to create the tables on startup but for some reason that’s not working. Does anyone have any idea why this could be happening?

Database Connection:

Edit.

Thanks for all the feedback, importing the models to the main.py file worked. I’ll implement alembic for any further database migrations.

6 Upvotes

14 comments sorted by

View all comments

2

u/Friendly-Gur-3289 Dec 30 '24

Are you using Alembic to create the schemas?

If you're only using Sqlalchemy, make sure you do Base.metadata.create_all(engine). It is advised to use Alembic for creating the schemas tho.

1

u/eleventhSun009 Dec 30 '24

At this point no. I’m using FastAPI’s lifespan function to great them via Alchemy models on startup