Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy: Get only one column [duplicate]

I'm trying to get all the rows in a table, but only one column. The model looks like this:

class Person:
    id= db.Column(db.String(11), primary_key=True, nullable=False)
    name = db.Column(db.String(255), nullable=False)
    city = db.Column(db.String(255), nullable=False)

I want to get the name column ONLY but I can't find anything on how to do this. I've tried stuff like:

Person.query.get(person.name)

The SQLAlchemy documentation doesn't help at all.

like image 677
ScandinavianWays Avatar asked May 16 '18 09:05

ScandinavianWays


1 Answers

You need to use with_entities to specify the column that you want to SELECT:

Person.query.with_entities(Person.name).all()
like image 109
Sicco Avatar answered Sep 21 '22 08:09

Sicco