4
多個外鍵關係,我有以下Peewee PostgreSQL的模型:保存Peewee查詢與對同桌
class Game(Model):
class Meta:
database = db
db_table = 'd_games'
game_id = PrimaryKeyField()
location = CharField()
home_team = ForeignKeyField(Team, related_name='home_games')
away_team = ForeignKeyField(Team, related_name='away_games')
你可以看到,該模型有一個團隊模型中的兩個外鍵關係。
我想要做的事,如:
不幸的是,雖然home_team
關係被保存在初始查詢:
# from the query log
'SELECT t1."game_id", t1."game_date", t1."location", t1."home_team_id", t1."away_team_id", t2."team_id", t2."team_name"
FROM "d_games" AS t1
INNER JOIN "d_team" AS t2
ON (t1."home_team_id" = t2."team_id")
LIMIT 10
的away_team
關係沒有,那麼一個新的查詢越來越每次我嘗試打印出g.away_team.team_name
時都會執行。
我該如何解決這個問題,以便保存home_team
和away_team
關係?我試圖
games = Game.select(Game, Team, Team).join(Team).switch(Game).join(Team).limit(10)
但是,這給了我一個table name "t2" specified more than once
錯誤,因爲這Peewee嘗試執行查詢
'SELECT t1."game_id", t1."game_date", t1."location", t1."home_team_id", t1."away_team_id", t2."team_id", t2."team_name", t2."team_id", t2."team_name"
FROM "d_games" AS t1
INNER JOIN "d_team" AS t2
ON (t1."home_team_id" = t2."team_id")
INNER JOIN "d_team" AS t2
ON (t1."home_team_id" = t2."team_id")
LIMIT 10