user964491 user964491 - 8 months ago 53
Python Question

use contains on a integer column with python peewee and postgresql

In my postgresql database there is a table which has a column with an integer data type. I would like to perform a .contains operation using the peewee python orm.

It does not seem to work since the integer value needs to be converted to a string before the comparison can happen. In postgresql you can do value::text and then the integer value will be a string and you can use it in a like expression.

Can i do this using the peewee orm, convert the value first and then use .contains? Or maybe there is another way?

For example i would like to match 20 out of 2022022


Answer Source

I see a couple answers, but I'll just add my own .02 as the developer of the library.

from playhouse.shortcuts import cast

query.where(cast(Url.http_status_code, 'text').contains(search_val))

I'm not sure which is more performant (probably TO_CHAR), but just in case you were looking for something more generic, you can use cast.