0

How do you get values from a Flask Query?

Here's what I'm trying:

batch_num_options = Ticket.query.values('batch_num').all()

and this produces the error:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) column "batch_num" does not exist LINE 1: SELECT batch_num

But if I psql into my Ticket table I can clearly see that batch_num is a column.

davidism
  • 121,510
  • 29
  • 395
  • 339
AlxVallejo
  • 3,066
  • 6
  • 50
  • 74

1 Answers1

0

I use it like this

our_model_object.query.with_entities(Your_model.your_attribute)

in your example, getting the first row only

batch_num = Ticket.query.with_entities(Ticket.batch_num).first() # or all() if you want all rows
print(batch_num)

This will return just the specified column, in this case batch_num. Or you can query the complete row, like this

ticket = Ticket.query.first() # use all if you want to get all rows
print(ticket.batch_num)
Matthew Story
  • 3,573
  • 15
  • 26
Felipe Emerim
  • 363
  • 2
  • 12