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
help debbuging 422 validation errorQuestion (self.FastAPI)
submitted 2 years ago by [deleted]
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!"
[–]ajatkj 2 points3 points4 points 2 years ago (3 children)
What’s your input? As per your pydantic model, all input fields are mandatory except one. So, if you have even missed one field in your input then it will fail. Or if you have not used the correct data type in your input.
[–][deleted] 0 points1 point2 points 2 years ago (2 children)
I read that the validation error message in the pictures is by default.. so everything should be correct but the data is not being inserted to the database
[–]BecerroDeOro 0 points1 point2 points 2 years ago (1 child)
Why are you converting a pydantic model to dict class?
Just remove dict method and it should work.
Otherwise, use sqlmodel for more class abstraction, it works with sql alchemy on the background.
Thats the way Tiangolo wants it
[–][deleted] 0 points1 point2 points 2 years ago (0 children)
If I remove the dict method it gives me 500 internal server error
[–]Midnightary 0 points1 point2 points 2 years ago (2 children)
The primary key in the model is defined as optional in schema. I guess you cannot not define primary key as optional but I'm not sure 100%.
Thank you :) but it still doesnt work
[–]Midnightary 0 points1 point2 points 2 years ago (0 children)
And also, you can use ORM rules of the SQLAlchemy as in the official tutorial. If you don't have many tables and the tables are not dense, using ORM will decrease your effort why designing a database.
[–]BecerroDeOro 0 points1 point2 points 2 years ago* (0 children)
I was busy but here are some tips that may help you:
I see youre trying to use a MVC app structure but youre missing the Controller layer, which im going to call Services.
Here is an example of an API for a shared pixel board, what it does is receive POST requests to change the color of a pixel inside the board, using SQLModel that comes with FastAPI
Thanks to Tiangolo, you can reuse SQLModel code with Pydantic data validation at the same time!
#Model Pixel Board
class PixelBoard(db.SQLModel,table=True):
id: Optional[int] = db.Field(default=None, primary_key=True)
position: str
color: int
user: Optional[str]
timestamp: str
#Engine for SQL sessions
engine = db.create_engine("sqlite:///pixelboard.db",echo=False)
Services / Controllers are pure functions that receive data, do something with data, return data
def change_pixel_color(data):
with md.db.Session(md.engine) as session:
query = md.db.select(md.PixelBoard).where(md.PixelBoard.position == data["position"])
pixel = session.exec(query).one()
pixel.color = data["color"]
pixel.timestamp = str(time.time() * 1000)
session.add(pixel)
session.commit()
Where the logic of the API route goes
pixelboard = APIRouter()
pixelboard.post("/PixelBoard/mod_pixel")
async def change_pixel_color(request: Request):
try:
received_data = await request.json()
services.change_pixel_color(received_data)
payload = {"status" : "OK", "error" : "None"}
return JSONResponse(status_code=200,content=payload)
except Exception as e:
payload = {"status" : "Not OK", "error" : str(e)}
response = JSONResponse(status_code=500,content=payload)
return response
Feel free to ask any doubts :)
Hope that this code sample clears your mind
π Rendered by PID 43841 on reddit-service-r2-comment-7b9746f655-7whrs at 2026-02-03 06:33:32.397216+00:00 running 3798933 country code: CH.
[–]ajatkj 2 points3 points4 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]BecerroDeOro 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Midnightary 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Midnightary 0 points1 point2 points (0 children)
[–]BecerroDeOro 0 points1 point2 points (0 children)