pydantic-sqlalchemy icon indicating copy to clipboard operation
pydantic-sqlalchemy copied to clipboard

orm_mode ignored (when using with FastAPI)?

Open JustinGuese opened this issue 2 years ago • 0 comments

looking at the script, I would expect orm_mode to be turned on by default, but when using the model I am getting the usual "AttributeError: 'dict' object has no attribute 'email'"?

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True, index=True, autoincrement=True, unique=True)
    email = Column(String, index=True, unique=True)
    organization_id = Column(Integer, ForeignKey("organizations.id"), index=True, nullable=True)
    role = Column(ENUM(Role), nullable=False, default=Role.ADMIN)

UserPD = sqlalchemy_to_pydantic(User)

[...]

@app.get("/users")
def getUsers(db: Session = Depends(get_db)): -> List[UserPD]
    users = db.query(User).all()
    users = [UserPD.from_orm(u) for u in users]
    # this one would fail
    print(users[0].email)
    # this one would work
    print(users[0]["email"]

JustinGuese avatar Aug 01 '23 13:08 JustinGuese