Pritam Bohra
Pritam Bohra

Reputation: 4319

Querying selected fields based on a list of string from database/model in SQLAlchemy

I am trying to create a GraphQL resolver using Python. I need to select only those columns from the database that the user has requested. I have the requested column names as a list of strings.

How do I query only those columns on that model using SQLAlchemy I tried the following, but that didn't work:

......
query = session.query(MyModel)
query = query.options(Load(MyModel).load_only(*requested_columns))
result = query.all()

Unfortunately this code is not working. Where am I going wrong?

Upvotes: 0

Views: 114

Answers (1)

gbottari
gbottari

Reputation: 11

At first glance, I don't think you need to call Load(My Model) because it is implied by query(MyModel). Have you tried simply: query.options(load_only(*requested_columns))?

Upvotes: 1

Related Questions