peewee select where not working when using a model instance rather than a model directly

人走茶凉 提交于 2021-02-10 19:51:35

问题


I have an instance of a peewee Model my_table = MyTable() from which I want to select some model instances.

I don't understand why this works:

In  [0] [selection.p_name for selection in my_table.select() if selection.p_type == "Solar"] 
Out [0] ['Solar, photovoltaic',
         'Solar, photovoltaic',
         'Solar, photovoltaic',
         'Solar, photovoltaic',
         'Solar, concentrated solar power',
         'Solar, concentrated solar power']

but this doesn't:

In  [1] selections = my_table.select().where(my_table.p_type=="Solar")
In  [2] [t.p_name for t in selections]
Out [2] 

Nothing is output. In fact, len(selections)=0

Am I doing something wrong?

My model definition is in one file and is as follows:

cafe3db = SqliteDatabase(db_fp)

class Cafe3BaseModel(Model):
    class Meta:
        database = cafe3db
class ScenarioTable(Cafe3BaseModel):
    pathway_scenario_key = CharField(primary_key=True)
    pathway_type = CharField()
    pathway_name = CharField()

cafe3db.create_tables([ScenarioTable])

I then populate the tables. Here is a screen capture of the SQLite database as seen DB Browswer for SQLite:

I then create an instance of the table: scenario_table = ScenarioTable()

Then, in a Python shell, I import the instance:

from x.y import scenario_table

I know it has all the model instances I expect (112):

>>> len(scenario_table.select())
112

And this works:

>>> [t.pathway_name for t in scenario_table.select() if t.pathway_type == 'Coal']
['Coal, sub-bituminous', 'Coal, bituminous', 'Coal, lignite', 'Coal, sub-bituminous', 'Coal, lignite', 'Coal, bituminous', 'Coal, bituminous', 'Coal, lignite']

But this doesn't:

>>> [t.pathway_name for t in scenario_table.select().where(scenario_table.pathway_type == 'Coal')]
[]

After trial and error, I was able to make things work by importing the model directly rather than an instance of the model. So, rather than:

    from x.y import scenario_table

I now have:

    from x.y import ScenarioTable  

and now:

selections = ScenarioTable.select().where(ScenarioTable.pathway_type=='Coal')
[t.pathway_name for t in selections]    

Does return the expected list of model instance names.

So my question is now: why does the model instance select work, but not the model instance where?


回答1:


I'm trying to reproduce your problem, but I can't.

First I create the database and the table:

import peewee
db = peewee.SqliteDatabase('test.db')
db.connect()

class my_table(peewee.Model):
    p_name = peewee.CharField()
    p_type = peewee.CharField()

    class Meta:
        database = db
db.create_tables([my_table])

Then I insert two rows:

d1 = my_table(p_name="Solar, photovoltaic", p_type="Solar")
d1.save()
d2 = my_table(p_name="Windmill", p_type="Wind")
d2.save()

After that I try your command:

>>> [t.p_name for t in my_table.select().where(my_table.p_type=="Solar")]
['Solar, photovoltaic']

So it works here - maybe you're doing something else wrong, but the code you provided is correct



来源:https://stackoverflow.com/questions/51049195/peewee-select-where-not-working-when-using-a-model-instance-rather-than-a-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!