繁体   English   中英

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

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

我是peewee的新手,目前正尝试从普通的Python SQlite3库迁移。

虽然我的代码生成了一个有效的SQL查询,该查询使用SQlite DB浏览器返回了预期的结果,但尝试获取字段的值返回AttributeError: x object has no attribute y

模型:

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查询:

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

产生的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

结果dicts():

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

为什么要运行此:

for item in data:
    print item.title

返回此:

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

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

您可以通过item.sonarr_series_id.title访问数据

您可能会考虑为您的字段命名一些更Python的名称。

暂无
暂无

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

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