Hacker News new | past | comments | ask | show | jobs | submit login

So how does it get from DB --> JSON response? SQLAlchemy or dbapi?



Yeah, FastAPI uses SQLAlchemy under it. Along with pydantic to define schemas with typing. And then just started tinkering with orjson for the json serialization. Seems to be the fastest library at the moment.

I have also been experimenting with encode/databases for async DB access. It still uses the SA core functions, which is nice, but that means it does not do the nice relationships stuff that SA has built in when using it to handle everything. At least not that I have found. However it does allow for things like gets without relationships, updates of single records, and stuff like that quite nicely.


I see, thanks. Is it required to define models twice as this page seems to recommend?

https://fastapi.tiangolo.com/tutorial/sql-databases/


FastAPI is database agnostic, although tutorials talk about using SQLAlchemy (probably because it's most popular).

I am using asyncpg[1] (much more performant and provides close mapping to PostgreSQL, making it much easier to use its advanced features) through raw SQL statements without problems.

[1] https://github.com/MagicStack/asyncpg


Yeah, you do end up defining everything more than once, once for SA, and then for pydantic. Create, Read, and Update may all be different pydantic models as well. They are for defining what comes in and out of the actual API. Your create request may not have the id field yet and some optional fields, and then the response has everything. And then an update may have everything as optional except the id. Only been using it a few weeks now, but liking it a lot so far.

https://fastapi.tiangolo.com/tutorial/sql-databases/#create-...


> FastAPI uses SQLAlchemy under it

This is somewhat inaccurate. They use SQLAlchemy in the tutorial, but FastAPI is in no way tied to SQLAlchemy.


Valid point. Should have said "can use".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: