pastyj
pastyj

Reputation: 105

Flask-SQLAlchemy Query Returns the Database Class and the Data

When I query my Database using .first() the Database returns a class

idtest = AddCompanyData.query.order_by(AddCompanyData.company_guid.desc()).first()

returns: <AddCompanyData 2>

The number "2" is correct but why won't it return just the number.

Upvotes: 1

Views: 1279

Answers (1)

Elisabeth Strunk
Elisabeth Strunk

Reputation: 566

That is because SQLAlchemy is usually used as an ORM (object-relational mapper). That means rows in tables are represented as objects in Python. If you query your database, SQLAlchemy will give you the result as such an object. If you want to access the value of a specific property/column, you can use the . operator. In your example e.g.

idtest.id

will give you the value of the id attribute/column.

Upvotes: 2

Related Questions