Posts

Showing posts from June, 2025

SQL concepts to define entities relationship

Image
SQL concepts to define entities relationship In SQL database is divided into tables and tables maintain relationship with each other. This relationship help us to minimize data duplicity and even help to connect one table to another logically and even help to utilize them efficiently. What is a ONE to MANY Relationship? We will try to explain this concept with respect to our current project. A user can have many todos. todos cannot have multiple users. Relationship diagram: Users and Todos table structure for this application: We need to modify these table a little bit to create relationship between these two tables. To make it possible we need to introduce new column to Todos table which will be Primary key of Users table as Foreign Key of Todos table as named owner[FK]. Logical representation through diagram: Sample data for testing the scenario: What is a foreign key? A foreign key (FK) is a column within a relational database table that provides a link between two separate tables. ...

Code Scalability and Routing

Image
 Creating Authentication file As we need to keep authentication part separate from our main application working so that we will not mess-up with our main application logic and it's security settings and authentication criteria as well as logics. auth.py - Basic authentication code stub. # This file is to define authenticator and authorization for the Todo application. from fastapi import FastAPI , status app = FastAPI () @ app . get ( "/auth" , status_code = status . HTTP_200_OK ) async def get_user ():     return { 'user' : 'authenticated_user' } Create Routers Package Here we want to use both and auth.py and main.py file to run together and to allow routers to run smoothly and want to scale the project code. To make this possible we need to use router facility or FastAPI. Here we are going to have a main file is a route of our application and routes will sit on this main file and will help in performing the tasks on call from main file. To make i...

API Request Method - Delete (Todos table record deletion)

Image
 API Request Method - Delete (Todos table record deletion) Code: # This file it to perform all the activity which we required for Todo application. # Depends means dependency injection from fastapi import FastAPI , Depends , HTTPException , Path import models from models import Todos from database import engine , SessionLocal from typing import Annotated from sqlalchemy . orm import Session from starlette import status from TodoValidator import TodoRequest app = FastAPI () # it will execute only when todos.db is not existing. # side effect if you enhance the table in model file then it will not update # it in database automatically. models . Base .metadata.create_all( bind = engine )   # creating db dependency def get_db ():     # We need to fetch this SessionLocal before each request made to database.     db = SessionLocal ()     try :         # Only the code prio to and including the     ...

API Request Method - PUT (Update the table)

Image
 API Request Method - PUT (Update the table) code: Path parameter validation variable must be last entry in method else it will cause error for other variable. # This file it to perform all the activity which we required for Todo application. # Depends means dependency injection from fastapi import FastAPI , Depends , HTTPException , Path import models from models import Todos from database import engine , SessionLocal from typing import Annotated from sqlalchemy . orm import Session from starlette import status from TodoValidator import TodoRequest app = FastAPI () # it will execute only when todos.db is not existing. # side effect if you enhance the table in model file then it will not update # it in database automatically. models . Base .metadata.create_all( bind = engine )   # creating db dependency def get_db ():     # We need to fetch this SessionLocal before each request made to database.     db = SessionLocal ()   ...

API Request Method - Post Todos (create and save todo)

Image
 API Request Method  - Post Todos (create and save todo) Code to make is properly: TodoValidator.py # Valdator class for Todos table entry for ensuring right data entry. from pydantic import BaseModel , Field # We have added id as part of validation # As it will be done by table and fastAPI by itself # As id is primary key and have incremental nature. # Even sqlalchemy will help us in doing it automatically. class TodoRequest ( BaseModel ):     title : str = Field ( min_length = 3 )     description : str = Field ( min_length = 3 , max_length = 100 )     priority : int = Field ( gt = 0 , lt = 6 )     complete : bool main.py: # This file it to perform all the activity which we required for Todo application. # Depends means dependency injection from fastapi import FastAPI , Depends , HTTPException , Path import models from models import Todos from database import engine , SessionLocal from typing import Annotated from...

API Request Methods - Get Todos from Database

Image
 API Request Methods - Get Todos from Database Fetching all records: Fetch Todos and dependency inversion. code: # This file it to perform all the activity which we required for Todo application. # Depends means dependency injection from fastapi import FastAPI , Depends import models from models import Todos from database import engine , SessionLocal from typing import Annotated from sqlalchemy . orm import Session app = FastAPI () # it will execute only when todos.db is not existing. # side effect if you enhance the table in model file then it will not update # it in database automatically. models . Base .metadata.create_all( bind = engine )   # creating db dependency def get_db ():     # We need to fetch this SessionLocal before each request made to database.     db = SessionLocal ()     try :         # Only the code prio to and including the         # yield statement will execute befo...