本文介绍了使用SqlAlChemy连接同一表的两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Flask中使用SqlAlChemy构建一个与体育相关的应用程序。我有一个游戏表,其中home_teamaway_team都加入了团队表。

我希望能够声明团队表和游戏表之间的关系,但我收到错误消息:

模型声明如下:

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_idhome_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_teamgame.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连接同一表的两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 05:41