我正在Sqlalchemy中与“多对多”关系作斗争。 我的数据模型:
class Chain(Base):
__tablename__ = "chain"
__table_args__ = {'extend_existing': True}
__guarded__ = ['id']
id = Column(Integer, primary_key=True)
name = Column(Text, nullable=False)
# .. some other fields
class JobConfig(Base):
__tablename__ = "jobconfiguration"
__table_args__ = {'extend_existing': True}
__guarded__ = ['id']
chain = relationship("Chain", secondary='chain_job')
id = Column(Integer, primary_key=True)
jobname = Column(String(250).evaluates_none(), nullable=False, unique=True)
# some other fields
class ChainJob(Base):
__tablename__ = "chain_job"
__table_args__ = {'extend_existing': True}
__guarded__ = ['id']
id = Column(Integer, primary_key=True)
chain_id = Column(Integer, ForeignKey('chain.id'))
job_id = Column(Integer, ForeignKey('jobconfiguration.id'))
现在,当我尝试创建新链并追加新作业时,出现错误消息:
chain = Chain()
chain.name='MyFirstChain'
job = JobConfig()
job.jobname="PartOfChain1"
job.chain.append(chain)
session.add(job)
session.add(chain)
session.commit()
sqlalchemy.orm.exc.UnmappedColumnError:无法执行以下同步规则 源列“ jobconfiguration.id”;映射器的映射类 JobConfig-> jobconfiguration”不会映射此列。尝试使用 不包含目的地的显式
foreign_keys
集合 列“ chain_job.job_id”(或使用viewonly = True关系)。
我该如何解决?