python - 如何使用 SQLAlchemy 进行此查询
问题描述
我有一个基本查询,需要为位置添加一个过滤器。
Location.parents
是一个整数数组。
它有效,但它使用 2 个查询:
parents_ids = self._db_session.query(Location.parents)\
.filter(Location.id == location_id)\
.scalar()
query = query.filter(
or_(
BookingItem.location_id == location_id,
BookingItem.location_id.in_(parents_ids)
)
)
我怎样才能用一个查询来做到这一点?我尝试使用子查询,但它不起作用。
parents_ids = self._db_session.query(Location.parents)\
.filter(Location.id == location_id)\
.subquery()
query = query.filter(
or_(
BookingItem.location_id == location_id,
BookingItem.location_id.in_(parents_ids.as_scalar())
)
)
解决方案
如果您希望 SQL 看起来像这样:
SELECT id
FROM booking_item
WHERE location_id = ?
OR (SELECT location_id = ANY(parents) FROM location WHERE id = ?)
那么相关的 SQLAlchemy 将是
parents_ids = self._db_session\
.query(BookingItem.location_id == Location.parents.any())\
.filter(Location.id == location_id)\
.subquery()
query = query.filter(
or_(
BookingItem.location_id == location_id,
parents_ids.as_scalar()
)
)
另一个选项是用于UNNEST
创建选项列表:
SQL:
SELECT id
FROM booking_item
WHERE location_id = ?
OR location_id IN (SELECT UNNEST(parents) FROM location WHERE id = ?)
那么相关的 SQLAlchemy 将是
parents_ids = self._db_session\
.query(func.unnest(Location.parents))\
.filter(Location.id == location_id)\
.subquery()
query = query.filter(
or_(
BookingItem.location_id == location_id,
BookingItem.location_id.in_(parents_ids.as_scalar())
)
)
推荐阅读
- android - 如何将 EditText 滚动到布局的最顶部?
- javascript - 如何结合execute_scipt和WebdriverWait
- c# - CosmosDB nuget 包不可用
- javascript - 我可以在智能手机的浏览器上播放音频()吗?
- vue.js - 向 v-card 中的 Vuetify 按钮添加路由?
- matlab - Matlab cart2pol函数
- sudo - systemctl 无法启动。“Sudo systemctl start apasn”我不明白错误
- ios - 使用 Swift 的计时器只能倒计时 20 秒,而不是 20 分钟
- java - 用户的 OAuth2 令牌到 RestTemplate
- macos - 显示 TStringGrid 单元格的工具提示