Convert SQL query to peewee

128 Views Asked by At

I need convert a SQL query to peewee instructions.

My SQL is:

    parts_test = db.execute('''
    SELECT tokenID, rewardCount FROM Loot
    INNER JOIN Quests ON Loot.questID = Quests.questID
    INNER JOIN Rewards ON Loot.rewardID = Rewards.rewardID
    WHERE Quests.questID = ? AND tokenContract = ?
    ORDER BY rewardCount
    ''', (quest_id, f"{PARTS_ADDR}")
).fetchall()
1

There are 1 best solutions below

0
coleifer On

Your column names are not following the peewee conventions, so you may need to modify, but roughly the below code should be clear enough to give you what you need:

(Loot
 .select(Loot.token_id, Loot.reward_count)
 .join_from(Loot, Quests, on=(Loot.quest == Quest.id))
 .join_from(Loot, Rewards, on=(Loot.reward == Reward.id))
 .where((Quest.id == quest_id) & (Loot.token_contract == X))
 .order_by(Loot.reward_count))