SQL queries allow not just to get a bunch of columns as a result but form it into something usefult at the same time. Eg:
select Person.name + " likes " + Food.name as all_i_want_as_a_result from Food, Person where Food.id == Person.favourite_food
But how can I do the same with SQLAlchemy? Eg:
query = db.session.query(Food, Person).filter(Food.id == Person.favourite_food) <-- how do I modify this line?
for row in db.session.execute(query)
print row
The expected output would be:
Alice likes icecream
Bob likes pizza
You can query for arbitrary things inside .query()
:
query = db.session.query(Person.name.concat(" likes ").concat(Food.name).label("all_i_want_as_a_result"))
.filter(Food.id == Person.favourite_food)
print query
This prints:
SELECT "Person".name || ? || "Food".name AS all_i_want_as_a_result
FROM "Person", "Food"
WHERE "Food".id = "Person".favourite_food
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.