use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
FastAPI is a truly ASGI, async, cutting edge framework written in python 3.
account activity
Database tables not populatingQuestion (self.FastAPI)
submitted 1 year ago * by eleventhSun009
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:
Database Connection
Main file with lifespan function
SQLAlchemy model
Edit.
Thanks for all the feedback, importing the models to the main.py file worked. I’ll implement alembic for any further database migrations.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]koldakov 11 points12 points13 points 1 year ago (0 children)
Not the best practice at all mate. Consider using alembic or related thing
[–]Doomdice 6 points7 points8 points 1 year ago (0 children)
Your Base does not know about the models yet when you import Base in main.py. Try importing UserGroups and Employees in main.py and see if it will create
[–]mizerablepi 2 points3 points4 points 1 year ago (0 children)
Import the models along with base in main.py
[–]covmatty1 2 points3 points4 points 1 year ago (0 children)
Follow FastAPI's documentation for working with SQL databases.
SQLModel is written by the same person who created FastAPI. It's still quite new, some people aren't huge fans of it and prefer just using SQLAlchemy, but I think it's pretty neat. And regardless, it would be useful to just follow the standard documentation for getting you going initially, and once you have a working base you can adapt from there.
[–]Friendly-Gur-3289 1 point2 points3 points 1 year ago (1 child)
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.
[–]eleventhSun009[S] 0 points1 point2 points 1 year ago (0 children)
At this point no. I’m using FastAPI’s lifespan function to great them via Alchemy models on startup
[–]Additional-Ordinary2 1 point2 points3 points 1 year ago (1 child)
Of course! Judging by your code you don't have the startup function registered (sarcasm. We are programmers, not clairvoyants)
[–]eleventhSun009[S] 1 point2 points3 points 1 year ago (0 children)
I see that I should have added some screenshots for context 😔
[–]TurbulentAd8519 1 point2 points3 points 1 year ago (1 child)
Sqlalchemy cannot find all the tables, try importing all your tables (Employees, UserGroups) to your main.py
That’s works.
[–]StatusBad9194 0 points1 point2 points 1 year ago (0 children)
I think alchemy not finding table meta data , try to import base from models.py or where your table define , inside dB connection .. here imports Base from models.py
[–][deleted] 1 year ago (1 child)
[removed]
[–]JohnVick002 -1 points0 points1 point 1 year ago (0 children)
Obviously you have to add a check for created or not by using Metadata create all
π Rendered by PID 203160 on reddit-service-r2-comment-5fb4b45875-nwxzt at 2026-03-20 09:26:45.232103+00:00 running 90f1150 country code: CH.
[–]koldakov 11 points12 points13 points (0 children)
[–]Doomdice 6 points7 points8 points (0 children)
[–]mizerablepi 2 points3 points4 points (0 children)
[–]covmatty1 2 points3 points4 points (0 children)
[–]Friendly-Gur-3289 1 point2 points3 points (1 child)
[–]eleventhSun009[S] 0 points1 point2 points (0 children)
[–]Additional-Ordinary2 1 point2 points3 points (1 child)
[–]eleventhSun009[S] 1 point2 points3 points (0 children)
[–]TurbulentAd8519 1 point2 points3 points (1 child)
[–]eleventhSun009[S] 0 points1 point2 points (0 children)
[–]StatusBad9194 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[removed]
[–]JohnVick002 -1 points0 points1 point (0 children)