在 SQLAlchemy 中为 ORM 创建一对多关系

Creating One-to-Many with Relationship for the ORM in SQLAlchemy

我正在学习 SQLAlchemy,我今天阅读了很多关于关系的文章,包括关于 SO 的多篇文章。但是,none 我发现的示例很好地回答了这个问题,尽管我认为这将是处理关系时首先要回答的问题之一。

我有网页表单数据,其中很多是重复的——比如访问者浏览器用户代理字符串、提供表单的域和提供表单的域。我想保留这些数据,但显然将代理之类的东西存储在它们自己的 table 中,然后在表单数据 table 中保留一个 ID 更有意义。所以我有一个这样的特工 class:

class Agent(Base):
    __tablename__ = 'agents'
    __table_args__ = {'mysql_engine': 'InnoDB'}

    ID = Column(Integer, autoincrement = True, primary_key = True)
    UserAgent = Column(VARCHAR(256), nullable = False, unique = True)

    #UniqueConstraint('UserAgent')

    def __repr__(self):
        return "<Agent(UserAgent='%s')>" % (self.UserAgent)

然后我有一个表单数据class:

class Lead(Base):
    __tablename__ = 'leads'
    __table_args__ = {'mysql_engine': 'InnoDB'}

    ID = Column(String(32), primary_key = True)
    AgentID = Column(Integer, ForeignKey('agents.ID'), nullable = False)
    .... other non relational fields ....
    IsPartial = Column(Boolean, nullable = False)

    Agent = relationship('Agent', backref = backref('leads', uselist = True))

    def __repr__(self):
        return "<Lead(ID='%s')>" % (self.ID)

此时,SQLAlchemy 创建了我要求的所有 tables,我可以创建一个测试 Lead 实例:

testLead = Lead(ID='....', ...)

然后创建一个测试代理实例:

testAgent = Agent(UserAgent='...', leads=testLead)

testLead 实例化得很好。但是,代理实例化失败:

TypeError: Incompatible collection type: Lead is not list-like

并使用 testLead.Agent = [...] 结果:

AttributeError: 'list' object has no attribute '_sa_instance_state'

理想情况下,我希望能够使用 Agent 字符串实例化 Lead 对象。然后,当我使用 session.add 和 session.commit 让 ORM 将代理字符串添加到代理 table 时(如果丢失)。同样,当我实例化 Lead class 时,我希望能够使用:

lead = Lead(ID='...')

那么如果我使用:

lead.Agent

应该显示代理字符串。如果我正确阅读了文档,这将需要添加一些延迟加载设置。

有办法吗?如果没有,我该如何解决上述错误?

谢谢

您似乎混合了 1-many 关系的两个方面:您的代码与测试数据的工作方式就好像一个 Lead 有很多 Agents,而 relationship定义则相反。假设模型是正确的,下面应该有效:

testLead = Lead(ID='....', ...)
testAgent = Agent(UserAgent='...', leads=[testLead])

或:

testAgent = Agent(UserAgent='...')
testLead = Lead(ID='....', ..., Agent=testAgent)

至于你的问题的第二部分,你可以通过简单的方式实现它 python:

class Lead(Base):
    # ...

    @property
    def UserAgent(self):
        return self.Agent and self.Agent.UserAgent

    @UserAgent.setter
    def UserAgent(self, value):
        # @note: you might first want to search for agent with this UserAgent value, 
        # and create a new one only if one does not exist
        # agent_obj = object_session(self).query(Agent).filter(Agent.UserAgent == value).one()
        agent_obj = Agent(UserAgent=value)
        self.Agent = agent_obj

    @UserAgent.deleter
    def UserAgent(self):
        self.Agent = None

或者您可以使用 Association Proxy 的目的:

def _agent_find_or_create(user_agent):
    agent = session.query(Agent).filter(Agent.UserAgent==user_agent).first()
    return agent or Agent(UserAgent=user_agent)

class Lead(Base):
    # ...

    Agent = relationship('Agent', backref = backref('leads', uselist = True))
    UserAgent = association_proxy('Agent', 'UserAgent',
                                  creator=_agent_find_or_create)