SQLAlchemy setup
Installation
To install run
pip3 install sqlalchemy
pip3 install psycopgFile Structure
.
βββ sql_app
βββ __init__.py
βββ crud.py
βββ database.py
βββ main.py
βββ models.py
βββ schemas.pyGuidelines
Once installed we will create a file called
database.pyin our App folderThis file will handle our database connection
In this file we will have the following:
Now that this is set up, we will create a new file called
models.pyThis is where all our database tables will be created
Each model is a table
The file contains the following:
Now that this is created we will need to set up the following line in the
main.pyfile
Now this should be good to go
Every time we will interact with the database we will get a session
Once we are done interacting, the session will close
We can start setting up paths to our App using this code
By setting all of this up, SQLAlchemy will connect to the Database
Check if the tables exist
If they don't it will create them
If they do it will mode on
But now if we connect to the database we should see the table created from scratch

Cleanup
Just to keep everything clean, we will move the dependency code in our
database.pyfileThis keeps everything clean, all the database stuff with the database stuff
Additionally we will have to import this in our
main.pyfile
database.py
main.py
Last updated