Reputation: 90
I'm making a GraphQL api with strawberry using SQLModel as the database manager. I'm trying to get a list of items where a substring is in a string so I did this
def resolve_user(id: int):
with Session(engine) as session:
user = session.exec(
select(UserModel).where(UserModel.id == id)
).one()
return create_user_instance(user)
# I belive the error comes from here
def resolve_users_by_name(name: str):
with Session(engine) as session:
users = session.exec(
select(UserModel).where(name in UserModel.name)
).all()
return [create_user_instance(user) for user in users]
@strawberry.type
class Query:
user: User = strawberry.field(resolver=resolve_user)
users_by_name: list[User] = strawberry.field(
resolver=resolve_users_by_name
)
When calling the usersByName
query I get the error Operator 'contains' is not supported on this expression
. I'm guessing it has to do with the name in UserModel.name
. I also looked if SQLModel had a LIKE
operator similar to SQL but I couldn't find anything. How can I solve this?
Upvotes: 0
Views: 3082
Reputation: 256
The line that is having the issue is:
select(UserModel).where(name in UserModel.name)
Try changing it to this format:
select(UserModel).where(UserModel.name.contains(name))
Sqlalchemy's comparator class has a set of methods you have to leverage, instead of vanilla python.
(I think this is a related thread)
Upvotes: 0