how to add constant column in Pypika

113 Views Asked by At

I ran into trouble. I've read through manual to pypika and I haven't found out, how to define constant column.

E.g.

SELECT t.id, 
       'constant' as constant_variable 
     FROM example_table t

I've tried using PseudoColumn, but it didn't work for me.

car = PseudoColumn('CAR') 

   query = Query \
        .from_('test_table') \
        .select(car) 
print(query)

The result is

SELECT CAR FROM "test_table"

Instead of

SELECT 'CAR' FROM "test_table"
1

There are 1 best solutions below

0
Jan Krejčí On

One of the ways to do it is to use ValueWrapper from pypika.terms. Same as in example above, but instead of PseudoColumn('CAR') use ValueWrapper('CAR', 'alias').

from pypika.terms import ValueWrapper
car = ValueWrapper('CAR', 'car') 

query = Query \
        .from_('test_table') \
        .select(car)