Skip to content

Instantly share code, notes, and snippets.

@tearf001
Created August 12, 2021 16:15
Show Gist options
  • Save tearf001/9944ca3cd4d8099615f498e4d95621b6 to your computer and use it in GitHub Desktop.
Save tearf001/9944ca3cd4d8099615f498e4d95621b6 to your computer and use it in GitHub Desktop.
sqlalchemy joinload
#! 作为数据延迟加载,但作为Instrument 此语法不支持... 包括Query也不支持跳
test = dashboard_dao.get_safe(role_id, joinedload=(Dashboard.portal, Dashboard.portal.project)) # !!! 执行失败
q = role_dao._buildQueryfields(joinedload=(Role.org, Organization.org_users)).filter(role_dao.model.id == role_id)
logger.info(f'{q}')
logger.info(f'{q.all()}') # 自动distinct ? 查询返回一条!
logger.info(f'{q.one_or_none()}') # 自动distinct ? 不会报错!
# 自定义
test = dashboard_dao.get_safe(role_id, joinedload=(Dashboard.portal, DashboardPortal.project)) # 多次预加载,但注意保持对象不得有多行
logger.info('---------------------------------------------------------------------1-----------------------------------------------------------------')
role: Role = role_dao.get_safe(role_id, joinedload=(Role.org, Organization.org_users))
logger.info(f'---------------------------------------------------------------------role: {role}-----------------------------------------------------------------')
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment