【问题标题】:SQLAlchemy Using relationship()SQLAlchemy 使用关系()
【发布时间】:2013-02-20 16:32:48
【问题描述】:

我在这里使用 SQLAlchemy,试图创建几个表并将它们链接起来,但在实现时遇到了问题。

class Team(Base):

    __tablename__ = "teams"

    id = Column(Integer, primary_key=True)
    espn_team_id = Column(Integer, unique=True, nullable=False)
    games = relationship("Game", order_by="Game.date")

    def __init__(self, name):
        self.name = name
        self.espn_team_id = espn_team_id
        self.games = games

class Game(Base):
    __tablename__ = "games"

    id = Column(Integer, primary_key=True)
    espn_game_id=Column(Integer, unique=True, nullable=False)
    date = Column(Date)

    h_espn_id = Column(Integer, ForeignKey('teams.espn_team_id'))
    a_espn_id = Column(Integer, ForeignKey('teams.espn_team_id'))

我将它放在一个文件中,用于创建表格。然后在另一个文件中,我使用 insert() 函数将值放入两个表中。我想如果我有一个 espn_team_id 360 的团队,然后我将多个游戏放入游戏表中,其中 h_espn_id=360 或 a_espn_id=360,我应该能够做到:

a = Table("teams", metadata, autoload=True)
a = session.query(a).filter(a.c.espn_team_id==360).first().games 

它应该给我一个 ID 为 360 的团队玩过的所有游戏的列表。但是我得到了这个错误

AttributeError: 'NamedTuple' 对象没有属性 'games'

我在这里对 SQLAlchemy 或关系数据库有什么误解?

【问题讨论】:

    标签: python database sqlalchemy relationship relational


    【解决方案1】:

    首先,您不必创建另一个Table 对象,因为它可以作为Team.__table__ 使用。无论如何,您可以只查询映射的类,例如

    query = Session.query(Team).filter(Team.espn_team_id == 360)
    team360 = query.one()
    games = team360.games
    

    有关方法.one().first().all(),请参阅文档:http://docs.sqlalchemy.org/en/latest/orm/query.html

    【讨论】:

    • sayap 感谢您的帖子和您的第一点。第二点是我的错,我现在将对其进行编辑,我确实包含了 .first() 并且它不起作用。观看:打印 session.query(a).filter(acespn_team_id==360).first().games Traceback(最近一次调用最后):文件“”,第 1 行,在 中 AttributeError:'NamedTuple ' 对象没有属性 'games'
    • 我尝试按照您的方式进行操作:from table_def import Team, Game >>> print session.query(Team).filter(Team.espn_team_id==360).first().games [big错误消息] sqlalchemy.exc.InvalidRequestError:一个或多个映射器无法初始化 - 无法继续初始化其他映射器。最初的例外是:无法确定关系 Team.games 上的父/子表之间的连接条件。指定一个“primaryjoin”表达式。如果存在“secondary”,则还需要“secondaryjoin”。
    • 哦,是的,类映射不完整,因为表之间有两个外键。我认为你需要有Team.home_gamesTeam.away_games
    【解决方案2】:

    这是我找到的解决方案,花了太长时间才理解这一点......

    class Team(Base):
    
        __tablename__ = "teams"
    
        id = Column(Integer, primary_key=True)
        name = Column(String)
        espn_team_id = Column(Integer, unique=True, nullable=False)
    
        h_games = relationship(
                      "Game", 
                      primaryjoin="Game.h_espn_id==Team.espn_team_id", 
                      order_by="Game.date")
        a_games = relationship(
                      "Game", 
                      primaryjoin="Game.a_espn_id==Team.espn_team_id", 
                      order_by="Game.date")
    
        @hybrid_property
        def games(self):
            return self.h_games+self.a_games
    
        def __init__(self, name):
            self.name = name
            self.espn_team_id = espn_team_id
            self.h_games = h_games
            self.a_games = a_games
            self.games = games
    

    【讨论】:

      猜你喜欢
      • 2015-03-22
      • 2013-10-22
      • 1970-01-01
      • 2011-11-22
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多