[英]SQLAlchemy self-referencing model
I'm building a model that stores emails with a set of properties: 我正在建立一个模型,该模型存储具有一组属性的电子邮件:
email_list_table = Table('email_list', Base.metadata,
Column('email_id', String, ForeignKey('emails.id')),
Column('list_id', Integer, ForeignKey('lists.id'))
)
class Email(Base):
__tablename__ = 'emails'
id = Column(String, primary_key=True)
from_email = Column(String, nullable=False)
from_name = Column(String, nullable=False)
date = Column(DateTime, nullable=False)
in_reply_to = Column(String)
subject = Column(String, nullable=False)
content = Column(String, nullable=False)
lists = relationship("List", secondary=email_list_table)
references = relationship("Email") # This is the issue
def __repr__(self):
return "<Email(id='%s', from='%s', subject='%s')>" % (self.id, self.from_name, self.subject)
class List(Base):
__tablename__ = 'lists'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
email = Column(String, nullable=False)
description = Column(String, nullable=False)
def __repr__(self):
return "<List(id='%s')>" % (self.id)
I would like to represent the references
relationship as a list of emails. 我想将references
关系表示为电子邮件列表。 For example, an email can reference more than one other email and be referenced by more than one email. 例如,一封电子邮件可以引用多个其他电子邮件,并且可以被多个电子邮件引用。
In traditional SQL, I would use a second table with a composite primary key of a referrer_id
and referenced_id
and when querying I would join the two tables to get my list of referenced emails. 在传统的SQL中,我将使用第二个表,该表具有referrer_id
和referenced_id
的复合主键,并且在查询时,我将两个表结合起来以获取引用的电子邮件列表。 This would make it simple to find what emails are referenced by a given ID and what emails a given ID references. 这将使查找给定ID引用哪些电子邮件以及给定ID引用哪些电子邮件变得简单。
I looked at the docs on SQLAlchemy for Adjacency List Relationships but I'm not sure the example there applies to my model (I guess I could add a column for referrer_id
, but that wouldn't produce the same table structure as I expect, with the 'join' table). 我查看了关于邻接列表关系的 SQLAlchemy上的文档,但不确定该示例是否适用于我的模型(我想我可以为referrer_id
添加一列,但这不会产生与我期望的相同的表结构, “联接”表)。
What's the best/most correct way to produce the model I expect with SQLAlchemy? 用SQLAlchemy生成模型的最佳/最正确方法是什么?
I got to the answer I wanted by creating a second table and referencing it in the Email
class like this: 通过创建第二个表并在Email
类中引用它,我得到了想要的答案,如下所示:
email_references = Table('email_references', Base.metadata,
Column('referencer_id', String, ForeignKey('emails.id'), primary_key=True),
Column('referenced_id', String, ForeignKey('emails.id'), primary_key=True)
)
class Email(Base):
__tablename__ = 'emails'
id = Column(String, primary_key=True)
from_email = Column(String, nullable=False)
from_name = Column(String, nullable=False)
date = Column(DateTime, nullable=False)
in_reply_to = Column(String)
subject = Column(String, nullable=False)
content = Column(String, nullable=False)
lists = relationship("List", secondary=email_list_table)
references = relationship("Email",
secondary=email_references,
primaryjoin=id==email_references.c.referencer_id,
secondaryjoin=id==email_references.c.referenced_id
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.