Cross database join in sqlalchemy

前端 未结 1 1136
-上瘾入骨i
-上瘾入骨i 2020-12-15 07:24

Is there a way in SQLAlchemy to do cross-database joins. To be specific, here is my use case:

Schema

  1. db1.entity1
    1. entity1_id:
相关标签:
1条回答
  • 2020-12-15 07:37

    You probably need to pass the schema parameter to sqlalchemy.schema.Table. When using declarative base for ORM mapping, you can provide this extra parameter through the __table_args__ property on your classes.

    class Entity2(Base):
        __tablename__ = 'entity2' ## I tried combination of <db>.<table> with no success
        __table_args__ = {'schema': 'db2'}
        entity2_id = Column(Integer, primary_key=True) 
    
    class Entity1(Base):
        __tablename__ = 'entity1' ## I tried combination of <db>.<table> with no success
        __table_args__ = {'schema': 'db1'}
        entity1_id = Column(Integer, primary_key=True)
        entity2_id = Column(Integer, ForeignKey(Entity2.entity2_id))
        entity2 = relationship('Entity2')
    
    0 讨论(0)
提交回复
热议问题