简体   繁体   English

带连接的Peewee查询无法按预期工作

[英]Peewee query with join doesn't work as expected

I'm new to peewee and currently trying to migrate from normal Python SQlite3 library. 我是peewee的新手,目前正尝试从普通的Python SQlite3库迁移。

While my code generate a valid SQL query that return result as expected using a SQlite DB browser, trying to get the value of a field return AttributeError: x object has no attribute y . 虽然我的代码生成了一个有效的SQL查询,该查询使用SQlite DB浏览器返回了预期的结果,但尝试获取字段的值返回AttributeError: x object has no attribute y

Model: 模型:

class TableShows(BaseModel):
    sonarr_series_id = IntegerField(column_name='sonarrSeriesId', unique=True)
    title = TextField()

    class Meta:
        table_name = 'table_shows'


class TableHistory(BaseModel):
    sonarr_series_id = ForeignKeyField(TableShows, field='sonarr_series_id', column_name='sonarrSeriesId')

    class Meta:
        table_name = 'table_history'

Peewee Query: Peewee查询:

data = TableHistory.select(
        TableShows.title,
        TableHistory.sonarr_series_id
    ).join(
        TableShows
    ).order_by(
        TableShows.title.asc()
    )

Resulting SQL query: 产生的SQL查询:

SELECT "t1"."title", "t2"."sonarrSeriesId"
FROM "table_history" AS "t2"
INNER JOIN "table_shows" AS "t1" ON ("t2"."sonarrSeriesId" = "t1"."sonarrSeriesId")
ORDER BY "t1"."title" ASC

Resulting dicts(): 结果dicts():

{'title': u'Test title', 'sonarr_series_id': 1}

Why does running this: 为什么要运行此:

for item in data:
    print item.title

Return this: 返回此:

AttributeError: 'TableHistory' object has no attribute 'title'

http://docs.peewee-orm.com/en/latest/peewee/relationships.html#selecting-from-multiple-sources http://docs.peewee-orm.com/zh-CN/latest/peewee/relationships.html#selecting-from-multiple-sources

You access the data via item.sonarr_series_id.title 您可以通过item.sonarr_series_id.title访问数据

You might consider naming your fields something a bit more pythonic. 您可能会考虑为您的字段命名一些更Python的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM