Search code examples
pythonormpeewee

how to select with if statement by peewee


I am getting all categories and join category table with item_category which has post categories. my sql is like that

SELECT category.*,IF(item_category.category_id=NULL, 0,1) is_selected
FROM category 
LEFT JOIN item_category ON category.category_id=item_category.category_id AND item_id=1

i tried that but it did not work. because python throw error when use if

Category.select(Category, ItemCategory, fn.if(ItemCategory.category_id==None, 0, 1) )

what is the correct usage for if statement? Thanks.


Solution

  • i found a solution but i do not know it is best way. i changed 'fn.if' to 'fn.IF' and it worked

    Category.select(Category, ItemCategory, fn.IF(ItemCategory.category_id==None, 0, 1) )