多对多关联表上的 SQLAlchemy 关系
我正在尝试建立与另一个多对多关系的关系,代码如下所示:
from sqlalchemy import Column, Integer, ForeignKey, Table, ForeignKeyConstraint, create_engine
from sqlalchemy.orm import relationship, backref, scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
supervision_association_table = Table('supervision', Base.metadata,
Column('supervisor_id', Integer, ForeignKey('supervisor.id'), primary_key=True),
Column('client_id', Integer, ForeignKey('client.id'), primary_key=True)
)
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
class Supervisor(User):
__tablename__ = 'supervisor'
__mapper_args__ = {'polymorphic_identity': 'supervisor'}
id = Column(Integer, ForeignKey('user.id'), primary_key = True)
schedules = relationship("Schedule", backref='supervisor')
class Client(User):
__tablename__ = 'client'
__mapper_args__ = {'polymorphic_identity': 'client'}
id = Column(Integer, ForeignKey('user.id'), primary_key = True)
supervisor = relationship("Supervisor", secondary=supervision_association_table,
backref='clients')
schedules = relationship("Schedule", backref="client")
class Schedule(Base):
__tablename__ = 'schedule'
__table_args__ = (
ForeignKeyConstraint(['client_id', 'supervisor_id'], ['supervision.client_id', 'supervision.supervisor_id']),
)
id = Column(Integer, primary_key=True)
client_id = Column(Integer, nullable=False)
supervisor_id = Column(Integer, nullable=False)
engine = create_engine('sqlite:///temp.db')
db_session = scoped_session(sessionmaker(bind=engine))
Base.metadata.create_all(bind=engine)
我想做的是将计划与特定的客户主管关系相关联,尽管我还没有找到如何做它。通过查看 SQLAlchemy 文档,我发现了一些提示,导致 Schedule-Table 上出现了foreignkeyconstraint。
我如何指定关系才能使该关联发挥作用?
I am trying to build a relationship to another many-to-many relationship, the code looks like this:
from sqlalchemy import Column, Integer, ForeignKey, Table, ForeignKeyConstraint, create_engine
from sqlalchemy.orm import relationship, backref, scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
supervision_association_table = Table('supervision', Base.metadata,
Column('supervisor_id', Integer, ForeignKey('supervisor.id'), primary_key=True),
Column('client_id', Integer, ForeignKey('client.id'), primary_key=True)
)
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
class Supervisor(User):
__tablename__ = 'supervisor'
__mapper_args__ = {'polymorphic_identity': 'supervisor'}
id = Column(Integer, ForeignKey('user.id'), primary_key = True)
schedules = relationship("Schedule", backref='supervisor')
class Client(User):
__tablename__ = 'client'
__mapper_args__ = {'polymorphic_identity': 'client'}
id = Column(Integer, ForeignKey('user.id'), primary_key = True)
supervisor = relationship("Supervisor", secondary=supervision_association_table,
backref='clients')
schedules = relationship("Schedule", backref="client")
class Schedule(Base):
__tablename__ = 'schedule'
__table_args__ = (
ForeignKeyConstraint(['client_id', 'supervisor_id'], ['supervision.client_id', 'supervision.supervisor_id']),
)
id = Column(Integer, primary_key=True)
client_id = Column(Integer, nullable=False)
supervisor_id = Column(Integer, nullable=False)
engine = create_engine('sqlite:///temp.db')
db_session = scoped_session(sessionmaker(bind=engine))
Base.metadata.create_all(bind=engine)
What I want to do is to relate a schedule to a specific Client-Supervisor-relationship, though I have not found out how to do it. Going through the SQLAlchemy documentation I found a few hints, resulting in the ForeignKeyConstraint on the Schedule-Table.
How can I specify the relationship to have this association work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要映射
supervision_association_table
以便您可以创建与它的关系。我可能在这里掩盖了一些东西,但看起来既然你在这里有多对多,你真的不能有
Client.schedules
- 如果我说Client.schedules.append( some_schedule)
,它指向“supervision”中的哪一行?下面的示例为那些加入每个
SupervisorAssociation
的Schedule
集合的访问器提供了一个只读“rollup”访问器。association_proxy
扩展用于在方便时隐藏SupervisionAssociation
对象的详细信息。You need to map
supervision_association_table
so that you can create relationships to/from it.I may be glossing over something here, but it seems like since you have many-to-many here you really can't have
Client.schedules
- if I sayClient.schedules.append(some_schedule)
, which row in "supervision" is it pointing to?The example below provides a read-only "rollup" accessor for those which joins the
Schedule
collections of eachSupervisorAssociation
. Theassociation_proxy
extension is used to conceal, when convenient, the details of theSupervisionAssociation
object.