Replies: 3 comments
-
Hey, @Abufari! If I have a user table: from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
email = Column(String)
# other fields... And only want to get name and id, I can do it by querying directly with sqlalchemy, but it will return row objects: result = session.query(User.id, User.name).filter(...).all() This returns a list of Row objects, not User model instances. If we used a sqlalchemy model, we would have to query the whole thing: users = session.query(User).filter(...).all() This fetches all columns, which makes it less efficient (specially for many columns or columns with a large memory footprint |
Beta Was this translation helpful? Give feedback.
-
An option would be adding a |
Beta Was this translation helpful? Give feedback.
-
Hi @igorbenav |
Beta Was this translation helpful? Give feedback.
-
I'm not terribly experienced with CRUD operations using pydantic, but my understanding so far has been that the service layer returns the ORM, which you can either use for some further internal computation, or FastAPI will turn it into a Pydantic model to return in the endpoint. Now FastCRUD seems to short-circuit it by returning a dict or pydantic model directly, but never the original model. That's alright if you want to return it in the endpoint anyway, but might be limiting its use for internal handling.
Is that not one of the intended usages of FastCRUD? If not, why not? Do I have to do the manual join and filtering again myself if I want the ORM?
Beta Was this translation helpful? Give feedback.
All reactions