Cas*_*per 7 python sqlalchemy python-3.x
有没有办法在使用 SQLAlchemy 中的选项加载相关对象时指定排序顺序selectinload?
我的 SQLAlchemy 版本:1.2.10 我的 python 版本:3.6.6
一种方法是仅指定映射类中关系的默认顺序。在下面的示例中,类似的查询query(Example).options(selectinload(Example.related_items))将按列对急切加载的相关项进行排序id。
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Base = declarative_base()
class Example(Base):
__tablename__ = 'examples'
id = Column(Integer, primary_key=True)
related_items = relationship('RelatedItem', back_populates='example', order_by='RelatedItem.id')
class RelatedItem(Base):
__tablename__ = 'related_items'
id = Column(Integer, primary_key=True)
example_id = Column(Integer, ForeignKey('examples.id'), nullable=False)
example = relationship('Example', back_populates='related_items')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1925 次 |
| 最近记录: |