Python Peewee – How to select and alias a literal column value?

Question:

Given the following model:

class User(BaseModel):
    name = TextField()

How do I execute the following sql:

SELECT name, 'hello' as foo
FROM user

I’ve found that I can use peewee.Alias, but I’m not sure if this is the ideal approach:

>>> print(User.select(User.name, Alias('hello', 'foo'))
SELECT "t1"."name", 'hello' AS "foo" FROM "users" AS "t1"

Is there an official way to do this?

Asked By: Matthew Moisen

||

Answers:

Your example is not the correct way. Use Value:

User.select(User.name, Value('hello').alias('foo'))
Answered By: coleifer
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.