SQLAlchemyにデータベース間結合を行う方法はありますか?具体的には、これが私のユースケースです。
スキーマ
- db1.entity1
- entity1_id:主キー
- entity2_id:db2.entity2.entity2_idへの外部キー
- db2.entity2
- entity2_id:主キー
モデル
モデルには宣言型のスタイルを使用しています。
class Entity1(Base):
__tablename__ = 'entity1' ## I tried combination of <db>.<table> with no success
entity1_id = Column(Integer, primary_key=True)
entity2_id = Column(Integer, ForeignKey('db2.entity2.entity2_id'))
entity2 = relationship('Entity2')
class Entity2(Base):
__tablename__ = 'entity2' ## I tried combination of <db>.<table> with no success
entity2_id = Column(Integer, primary_key=True)
さて、予想通り、Entity1のクエリは失敗し、テーブルentity2が見つからないというMySQLエラーメッセージが表示されます。私は多くの異なる組み合わせを試しまし__tablename__
たが、成功しませんでした。だから私はSQLAlchemyでそれが可能かどうか疑問に思いました。