我相信这就是您正在寻找的(您只需要backref
在与 another 的多对多关系中定义一个关系attribute_mapped_collection
):
from sqlalchemy import Column, ForeignKey, Integer, String, Table, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship, sessionmaker
from sqlalchemy.orm.collections import attribute_mapped_collection
Base = declarative_base()
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
a_b_association_table = Table(
'a_b_association', Base.metadata,
Column('a_id', Integer, ForeignKey('a.id')),
Column('b_id', Integer, ForeignKey('b.id'))
)
class B(Base):
__tablename__ = 'b'
id = Column(String, primary_key=True)
a_instances = relationship(
A,
backref = backref(
'b_instances',
collection_class = attribute_mapped_collection('id'),
),
secondary = a_b_association_table,
collection_class = attribute_mapped_collection('id'),
)
engine = create_engine('sqlite://')
Base.metadata.bind = engine
Base.metadata.create_all()
Session = sessionmaker(bind=engine)
session = Session()
a1, a2, a3 = A(id=1), A(id=2), A(id=3)
b1, b2, b3 = B(id='a'), B(id='b'), B(id='c')
session.add_all([a1, a2, a3, b1, b2, b3])
session.flush()
print(a1.b_instances)
print(b1.a_instances)
a1.b_instances[b1.id] = b1
a1.b_instances[b2.id] = b2
b1.a_instances[a1.id] = a1
b1.a_instances[a2.id] = a2
session.flush()
session.expunge_all()
a1 = session.query(A).get(1)
b1 = session.query(B).get('a')
print(a1.b_instances)
print(b1.a_instances)
输出:
{}
{}
{u'a': <__main__.B object at 0x2816490>, u'b': <__main__.B object at 0x28b0350>}
{1: <__main__.A object at 0x28ac9d0>, 2: <__main__.A object at 0x28b0650>}