I have some trouble using the peewee library. I'm using sqlite database behind my python application combined with peewee to acces the database and on one of my function, i'm retrieving datas depending on the input parameters of my function, it looks like this:
def display_characters(self, menu_update: PageView123, emoji: Emoji = None):
# Characters retrieving
query = Character.select(Character, fn.Count(Character.id).alias('count'), CharactersOwnership.discord_user_id)
museum_filter = menu_update.retrieve_hidden_data()
if museum_filter.category is not None:
query = query.where(Character.category == museum_filter.category)
if museum_filter.rarity is not None:
query = query.where(Character.rarity == museum_filter.rarity)
if museum_filter.affiliation is not None:
query = (query.join(CharacterAffiliation)
.join(Affiliation)
.where(Affiliation.name == museum_filter.affiliation))
total_characters = query.count()
# Then we filter on only the owned card
query = (query.join(CharactersOwnership, on=(CharactersOwnership.character_id == Character.id))
.where(CharactersOwnership.discord_user_id == museum_filter.owner.id)
.group_by(Character.id)
.order_by(Character.name))
total_owned = query.count()
The code itself doesn't speak much, but the thing is that when i'm reaching the last step, the models retrieved are not complete when we enter the condition if museum_filter.affiliation is not None:
.
To describe my issue, here is what it looks like:
Any idea why the model that result from the query is not complete?
Thanks!
You might want to explicitly call .join_from()
specifying source and dest table, since peewee's "join context" may be hurting you rather than helping you. Doc here: http://docs.peewee-orm.com/en/latest/peewee/relationships.html#joining-multiple-tables
Alternatively, at the end of the affiliation block call .switch(Character)
to reset the join context to character. Additionally, you need to explicitly select()
all the columns you want retrieved. Just joining on additional tables does not imply selecting the data out of them