假设我有以下模型:
class Molecule(Base):
db = Column(Integer, primary_key=True)
id = Column(Integer, primary_key=True)
data = Column(Integer)
class Atom(Base):
id = Column(Integer, primary_key=True)
weight = Column(Integer)
我想建立一个多对多分子和原子之间的关系,最好的方法是什么?请注意,首要的关键分子的 是合成的.
Thanks
多对多关联表应该这样定义:
molecule2atom = Table(
'molecule2atom',
Base.metadata,
Column('molecule_db', Integer),
Column('molecule_id', Integer),
Column('atom_id', Integer, ForeignKey('atom.id')),
ForeignKeyConstraint(
('molecule_db', 'molecule_id'),
('molecule.db', 'molecule.id') ),
)
并像往常一样将关系添加到模型之一,例如在 Atom 类中添加:
molecules = relationship("Molecule", secondary=molecule2atom, backref="atoms")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)