簡體   English   中英

如何從SQLAlchemy中的現有關系創建關系?

[英]How to create relationships from existing relationships in SQLAlchemy?

我對sqlalchemy中的關系中的關系有些困惑。 我有一個這樣的模型:

engine = create_engine('sqlite:///tvdb.db', echo=True)
Base = declarative_base(bind=engine)

episodes_writers = Table('episodes_writers_assoc', Base.metadata,
    Column('episode_id', Integer, ForeignKey('episodes.id')),
    Column('writer_id', Integer, ForeignKey('writers.id')),
    Column('series_id', Integer, ForeignKey('episodes.series_id'))

class Show(Base):
    __tablename__ = 'shows'

    id = Column(Integer, primary_key = True)
    episodes = relationship('Episode', backref = 'shows')

class Episode(Base):
    __tablename__ = 'episodes'

    id = Column(Integer, primary_key = True)
    series_id = Column(Integer, ForeignKey('shows.id'))
    writers = relationship('Writer', secondary = 'episodes_writers',
                           backref = 'episodes')

class Writer(Base):
    __tablename__ = 'writers'

    id = Column(Integer, primary_key = True)
    name = Column(Unicode)

因此,節目和劇集之間是一對多的,而劇集和作家之間是多對多的,但是我現在想基於一個事實,即作家是一個作家,就是在節目和作家之間建立多對多的關系。與節目的情節相關。 我試圖將另一列添加到assoc表中,但這導致以下異常:

sqlalchemy.exc.ArgumentError: Could not determine join condition between parent/child tables on relationship Episode.writers.  Specify a 'primaryjoin' expression.  If 'secondary' is present, 'secondaryjoin' is needed as well.

所以我的關系聲明顯然做錯了。 如何創建正確的主連接來實現我在這里想要做的事情?

該錯誤消息中建議正確修復。 無論如何,您不需要使規范非規范化的列。 writer.shows可以是一個關聯代理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM