2

SQLAlchemy 的文档很棒,但有时对于来自 Rails 和 Active Record 的人来说有点压倒性。

鉴于此 Active Record 关系集,SQLAlchemy 等效于通过中间表建立关系吗?

具体给出下面的基本 Active Record 示例,我试图了解如何在 SQLAlchemy 中定义等效关系,该关系可以允许 Account SQLAlchemy 模型绑定到 AccountHistory 模型。我不清楚我是否应该使用映射器函数,我觉得我在 SQLAlchemy 中遗漏了一些简单的东西。

class Supplier < ActiveRecord::Base
  has_one :account
  has_one :account_history, through: :account
end

class Account < ActiveRecord::Base
  belongs_to :supplier
  has_one :account_history
end

class AccountHistory < ActiveRecord::Base
  belongs_to :account
end
4

1 回答 1

0

也许我误解了has_one_through. 回想起来,我认为您正在寻找这样的东西:

class Supplier(Base):
    __tablename__ = 'supplier'
    id = Column(Integer, primary_key=True)

class AccountHistory(Base):
    __tablename__ = 'account_history'
    id = Column(Integer, primary_key=True)
    account_id = Column(Integer, ForeignKey('account.id'))

class Account(Base):
    __tablename__ = 'account'
    supplier_id = Column(Integer, ForeignKey('supplier.id'))
于 2013-09-24T18:48:36.340 回答