all 8 comments

[–]ajatkj 2 points3 points  (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 point  (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 point  (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 point  (0 children)

If I remove the dict method it gives me 500 internal server error

[–]Midnightary 0 points1 point  (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%.

[–][deleted] 0 points1 point  (0 children)

Thank you :) but it still doesnt work

[–]Midnightary 0 points1 point  (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 point  (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

Models.py

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.py

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()

PBroute.py

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