on-internet
← Community

Python SQLModel FastAPI get unique values from database

Stack Overflow ·

I have this an API which should get all unique titles with limit of 5. @app.get( "/placeholder/{item_title}", response_model=list[ItemPublic] ) def suggest_item( *, session: Session = Depends(get_session), item_title: str ): item_interests = session.exec( select(Item) .distinct() .where(col(Item.title).ilike(f"%{item_title}%")) .limit(5) ).all() return item_interests I should actually be getting all unique titles that are stored in database based on what user types, however I can't make .distinct() work. I have tested this API and it returns duplicate titles. Where does my code go wrong?

Comments

Sign in or sign up to comment and upvote.

No comments yet.