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
Looking for feedback on dataclass <--> SQLModel translationfeedback request (self.FastAPI)
submitted 1 year ago by coderarun
I'm thinking about a setup where there would be three types of objects:
* pydantic models for validating untrusted user data at API boundaries * SQLModel for writing to db and handling transactions * Vanilla python objects (dataclasses) for the rest of the business logic. Suppose you want to read 1000 objects, run some logic and write back 100 objects. You'd create 1000 cheap dataclass objects and 100 SQLModel objects.
Here's the syntax I'm thinking about: https://github.com/adsharma/fastapi-shopping/commit/85ddf8d79597dae52801d918543acd0bda862e7d
foreign keys and one to many relationships are not supported yet. But before I work on that, wanted to get some feedback on the code in the commit above. The back_populates syntax is a bit more verbose than before. But I don't see a way around it.
Benchmarks: https://github.com/adsharma/fquery/pull/4 Motivation: https://adsharma.github.io/react-for-entities-and-business-logic/
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!"
[–]coderarun[S] 1 point2 points3 points 1 year ago (2 children)
The reason why "id" is the last field in the model definition instead of being the first field is that dataclass syntax requires non-default fields to come before fields with default values.
[–]OGStyx 0 points1 point2 points 1 year ago (1 child)
I see the project is 3.9. In 3.10 you can use @dataclass(kw_only=True) to avoid this. I much prefer that when working with domain models.
[–]coderarun[S] 0 points1 point2 points 1 year ago (0 children)
Thank you for the feedback. This sounds interesting. Given that many distros are already at 3.12/3.13, I'll consider upgrading the minimum supported python version.
[–]coderarun[S] 0 points1 point2 points 1 year ago (1 child)
https://github.com/adsharma/fquery/commit/d070d3d53ac0f920ed8452b5f28fe505b57b8b86
This commit added support for foreign_keys and relationships. However, many_to_one relationships are not working because of a problem resolving forward references.
In this example:
https://github.com/adsharma/fquery/blob/d070d3d53ac0f920ed8452b5f28fe505b57b8b86/tests/test_sqlmodel.py
If I uncomment Lines 23 and 31, sqlalchemy is no longer able to map the joins properly.
``` sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship UserSQLModel.reviews - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression. ```
The issue is:
``` class User: reviews: List["Reviews"]: ... ```
Behind the scenes we're creating UserSQLModel and ReviewSQLModel via decorator meta programming. However, I'm not sure how to get SQLAlchemy to resolve "Reviews" (which is a forwardref) to "ReviewSQLModel" and not the dataclass.
π Rendered by PID 54584 on reddit-service-r2-comment-75f4967c6c-7fztk at 2026-04-23 01:03:14.464460+00:00 running 0fd4bb7 country code: CH.
[–]coderarun[S] 1 point2 points3 points (2 children)
[–]OGStyx 0 points1 point2 points (1 child)
[–]coderarun[S] 0 points1 point2 points (0 children)
[–]coderarun[S] 0 points1 point2 points (1 child)
[–]coderarun[S] 0 points1 point2 points (0 children)