簡體   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