SQLAlchemy的多个连接

时间:2018-01-30 15:58:12

标签: python mysql sqlalchemy

我有这个数据库模型......

class Distributor(Base):  
    __tablename__ = 'distributor'
    id = Column(Integer, primary_key=True)
    commercial_address_id = Column(Integer, ForeignKey('addresses.id'), nullable=False, unique=True)
    invoice_address_id = Column(Integer, ForeignKey('addresses.id'), nullable=False, unique=True)
    commercial_address = relationship(Address, lazy="noload", foreign_keys=[commercial_address_id])
    invoice_address = relationship(Address, lazy="noload", foreign_keys=[invoice_address_id])

class Address(Base):  
    __tablename__ = 'address'
    id = Column(Integer, primary_key=True)
    address_name = Column(String(64))
    country_id = Column(Integer, ForeignKey('countries.id'), nullable=False)
    country = relationship(Country, lazy="noload")

class Country(Base):  
    __tablename__ = 'country'
    id = Column(Integer, primary_key=True)
    code = Column(Integer)

...然后我对数据库进行查询...

alias_commercial_address = aliased(Distributor.invoice_address)
alias_commercial_address_country = aliased(Address.country)
alias_invoice_address = aliased(Distributor.commercial_address)
alias_invoice_address_country = aliased(Address.country)

return Distributor.query\
    .outerjoin(alias_commercial_address, Distributor.commercial_address)\
    .outerjoin(alias_commercial_address_country, Address.country)\
    .outerjoin(alias_invoice_address, Distributor.invoice_address)\
    .outerjoin(alias_invoice_address_country, Address.country)\
    .options(contains_eager(Distributor.commercial_address, alias=alias_commercial_address)
             .contains_eager(Address.country, alias=alias_commercial_address_country),
             contains_eager(Distributor.invoice_address, alias=alias_invoice_address)
             .contains_eager(Address.country, alias=alias_invoice_address_country))\
    .all()

...但是框架错误地生成了查询......

SELECT ... FROM distributors
LEFT OUTER JOIN addresses AS addresses_1 ON addresses_1.id = distributors.commercial_address_id
LEFT OUTER JOIN countries AS countries_1 ON countries_1.id = addresses_1.country_id 
LEFT OUTER JOIN addresses AS addresses_2 ON addresses_2.id = distributors.invoice_address_id 
LEFT OUTER JOIN countries AS countries_2 ON countries_2.id = addresses_1.country_id

......最后的JOIN应该是......

LEFT OUTER JOIN国家AS countries_2 ON countries_2.id = addresses_2.country_id

我尝试过不同的方法来创建SQL查询,但我无法正确生成查询。知道我的错误在哪里?

谢谢!

1 个答案:

答案 0 :(得分:2)

将您的进一步联接基于您创建的别名:

Distributor.query\
    .outerjoin(alias_commercial_address, Distributor.commercial_address)\
    .outerjoin(alias_commercial_address_country, alias_commercial_address.country)\
    .outerjoin(alias_invoice_address, Distributor.invoice_address)\
    .outerjoin(alias_invoice_address_country, alias_invoice_address.country)\
    .options(contains_eager(Distributor.commercial_address, alias=alias_commercial_address)
             .contains_eager(alias_commercial_address.country, alias=alias_commercial_address_country),
             contains_eager(Distributor.invoice_address, alias=alias_invoice_address)
             .contains_eager(alias_invoice_address.country, alias=alias_invoice_address_country))\
    .all()