2

SQLAlchemy's docs are great but a little overwhelming at times for someone coming from Rails and Active Record.

Given this Active Record relationship set what would be the SQLAlchemy equivalent to establish a relationship through an intermediary table?

Specifically given the basic Active Record example below I'm trying to understand how to define an equivalent relationship in SQLAlchemy that can allow an Account SQLAlchemy model to be tied to an AccountHistory Model. I'm not clear if I'm supposed to be using a mapper function and I feel like I'm missing something simple in 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 に答える