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?
Answers:
Your example is not the correct way. Use Value
:
User.select(User.name, Value('hello').alias('foo'))
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?
Your example is not the correct way. Use Value
:
User.select(User.name, Value('hello').alias('foo'))