Skip to content
Advertisement

How to display list data from SqlAlchemy Query using fastAPI

So, I have been trying to learn Python Full-Stack web development using fastAPI. Most of my website works properly but I am having trouble with sending the list of Query using FASTAPI.

I get this error.

JavaScript

This is my FastAPI router code.

JavaScript

This is the function that returns the list of all comics stored in the database.

JavaScript

and my pydantic response class

JavaScript

I don’t know what I’m doing wrong here. I have read the FASTAPI help documentation and in there they also use List[PydanticClassName] to return a list of database queries, but for some reason, it is not working for me.

EDIT: So, I checked the response of list_comics() function. It does indeed return the right response. I tested it by printing the comic.title in a Jinja2 template.

JavaScript

So the error seems to be happening either in the pydantic class model or FastAPI router.

Advertisement

Answer

This solution suggested by @MatsLindh solved it.

That is not the endpoint being loaded; the error message complains about an endpoint with id set (i.e. /get/{id} or something similar). You probably have that endpoint defined before your /get/all endpoint, and since all is not an integer, it throws the given error.

This solved my problem, As I had another endpoint before the /get/all endpoint named as /get/{id} which was causing the error.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement