Two Columns joining the same table with SqlAlchemy(使用SqlAlChemy连接同一表的两列)
问题描述
我正在Flask中使用SqlAlChemy构建一个与体育相关的应用程序。我有一个游戏表,其中home_team
和away_team
都加入了团队表。
我希望能够声明团队表和游戏表之间的关系,但我收到错误消息:
Could not determine join condition between parent/child tables on relationship Team.games - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table.
模型声明如下:
class Team(db.Model):
"""Team model."""
__tablename__ = "teams"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
monkier = db.Column(db.String)
town = db.Column(db.String)
games = db.relationship('Game')
def __init__(self):
......
class Game(db.Model):
"""Game model."""
__tablename__ = "games"
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime)
ground_id = db.Column(db.Integer, db.ForeignKey('grounds.id'))
round_id = db.Column(db.Integer, db.ForeignKey('rounds.id'))
home_team_id = db.Column(db.Integer, db.ForeignKey('teams.id'))
away_team_id = db.Column(db.Integer, db.ForeignKey('teams.id'))
home_score = db.Column(db.String)
away_score = db.Column(db.String)
ground = db.relationship('Ground')
round = db.relationship('Round')
home_team = db.relationship('Team',
primaryjoin="Game.home_team_id == Team.id")
away_team = db.relationship('Team',
primaryjoin="Game.away_team_id == Team.id")
def __init__(self):
......
看起来我必须将foreign_keys
关键字参数传递给关系,但我不确定如何操作,因为away_team_id
和home_team_id
都需要这样做。
当然,这也可能是糟糕的数据库设计。
推荐答案
我认为您可以通过对模型进行一些调整来创建您要寻找的关系。
class Team(db.Model):
"""Team model."""
__tablename__ = "teams"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
monkier = db.Column(db.String)
town = db.Column(db.String)
home_games = db.relationship('Game', foreign_keys='Game.home_team_id', backref='home_team', lazy='dynamic')
away_games = db.relationship('Game', foreign_keys='Game.away_team_id', backref='away_team', lazy='dynamic')
def __init__(self):
......
@property
def games(self):
return self.home_games.union(self.away_games)
class Game(db.Model):
"""Game model."""
__tablename__ = "games"
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime)
ground_id = db.Column(db.Integer, db.ForeignKey('grounds.id'))
round_id = db.Column(db.Integer, db.ForeignKey('rounds.id'))
home_team_id = db.Column(db.Integer, db.ForeignKey('teams.id'))
away_team_id = db.Column(db.Integer, db.ForeignKey('teams.id'))
home_score = db.Column(db.String)
away_score = db.Column(db.String)
ground = db.relationship('Ground')
round = db.relationship('Round')
def __init__(self):
......
现在您可以通过Team
的关系中定义的backref
属性访问Team
模型。
例如game.away_team
或game.home_team
您还可以查询特定于球队的主场、客场或所有比赛。
all_home_games = team.home_games.all()
away_against_dodgers = team.away_games.filter_by(name='Dodgers').all()
some_date = DateTime(...)
all_games_before_date = team.games.filter(Game.date < some_date).all()
这篇关于使用SqlAlChemy连接同一表的两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用SqlAlChemy连接同一表的两列
基础教程推荐
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01