我有以下代表时间线的Table
模型。SQLAlchemy:如何查询自引用邻接列表表上的可选列?
class TimeRange(Base):
__tablename__ = "time_line"
record_id = Column(Integer, primary_key=True)
level = Column(String, nullable=False) # e.g. "Point", "Range"
content = Column(String, nullable=False)
language_marker = Column(String) # this one column is optional and needs to be queried
immediate_parent_id = Column(Integer, ForeignKey('time_line.record_id'))
child_timelines = relationship('TimeRange', backref=backref('parent_timeline', remote_side=[record_id]))
的language_marker
列是需要以递归方式要查询的一个。并非所有记录都具有这样的属性,并且业务逻辑是:沿着从根到子时间轴的层次结构沿袭至少一个级别的实例携带这样的属性,并且应该返回最低级别的属性。这有点像级联样式表,如果TimeRange
对象本身没有这样的属性,只需再往上看一级,util找到一个,并且最新定义的样式获胜。
我应该考虑实施这些查询的技术方向是什么?我使用的是SQLAlchemy,而后端是SQLite。谢谢。
当'language_marker'是'None'时,为父'TimeRange'选择查询性能好吗? – xli
@xli,谢谢你的回复。这绝对没问题。现在我对这里的技术方向毫无头绪。你能提供更多细节吗? –