从 Peewee 的列中选择不同的值

Selecting distinct values from a column in Peewee

我正在寻找 select 一列中使用 Peewee 不同的所有值。

例如,如果我有 table

 Organization     Year   
 company_1         2000
 company_1         2001
 company_2         2000
 .... 

仅 return 组织列中的唯一值 [即 company_1company_2]

我曾假设使用 distinct 选项可以实现 http://docs.peewee-orm.com/en/latest/peewee/api.html#SelectQuery.distinct

我当前的代码:

   organizations_returned = organization_db.select().distinct(organization_db.organization_column).execute()

    for item in organizations_returned:
         print (item.organization_column)

不会产生不同的行 returned(它会产生例如 company_1 两次)。

我试过的另一个选项:

  organization_db.select().distinct([organization_db.organization_column]).execute()

disctinct 选项中包含 [ ],虽然看起来与文档更一致,但导致错误 peewee.OperationalError: near "ON": syntax error:

我假设可以直接从 Peewee return 唯一值是否正确 - 如果是这样,我做错了什么?


模型结构:

cd_sql = SqliteDatabase(sql_location, threadlocals=True, pragmas=(("synchronous", "off"),))     

class BaseModel(Model):
    class Meta:
        database = cd_sql

class organization_db(BaseModel):
    organization_column = CharField()
    year_column = CharField()

所以 coleifer 的意思是 Sqlite 不支持 DISTINCT ON。不过这不是什么大问题,我想你可以像这样完成你想要的:

organization_db.select(organization_db.organization).distinct()