当前位置:网站首页>Sqlalchemy usage related
Sqlalchemy usage related
2022-07-28 06:07:00 【Alex_ z0897】
Paging during sub query
sub_query = db.session.query(Table,Table.id).offset((page_no - 1) * page_size).limit(page_size).subquery()
query1 = db.session.query(sub_query, Table4, Table3, Table2) \
.outerjoin(Table2) \
.outerjoin(Table3) \
.outerjoin(Table4) \
SQLAlchemy Inquiry exists
SELECT *
FROM table1
WHERE EXISTS (SELECT *
FROM (SELECT max(table2.event_type) AS event_type, table2.event_code AS event_code
FROM table2
WHERE table2.`status` = 200 GROUP BY table2.event_code) AS anon_1
WHERE table1.id = anon_1.event_code AND anon_1.event_type = 100 ) ORDER BY table1.create_time DESC
query = db.session.query(Table1).order_by(Table1.create_time.desc())
func_criteria = db.session.query(func.max(Table2.event_type).label('event_type'),
Table2.event_code.label('event_code'))\
.group_by(Table2.event_code).filter(Table2.status == 200).subquery()
query = query.filter(exists().where(Table1.id == func_criteria.c.event_code)\
.where(func_criteria.c.event_type== status))
Example of official website
>>> subq = (
... select(func.count(address_table.c.id)).
... where(user_table.c.id == address_table.c.user_id).
... group_by(address_table.c.user_id).
... having(func.count(address_table.c.id) > 1)
... ).exists()
>>> with engine.connect() as conn:
... result = conn.execute(
... select(user_table.c.name).where(subq)
... )
... print(result.all())
sqlalchemy Of update Manual commit Passed before yield When performing a remote asynchronous call session Object is replaced
SQLAlchemy==1.2.18
result = db.session.query(ObjectModel).get(ids.get("id"))
result.status = 2 # here session._is_clean() = False
# Execute once in the middle yield The remote invocation
# External request function
def onAyncFetch():
req = ... Omit other code
response = yield async_http.fetch(req)
... Omit other code
return Return(json_decode(response.body))
print id(result.query.session)
data = yield onAyncFetch(...)
print id(result.query.session) # The memory address of the two prints is different
... Omit other code
# perform yield At the end of the call session._is_clean() = True
db.session.commit() # Proceed again commit The following code segment will be carried out , It's modified above status Field cannot be commit
It's a piece of pseudo code . The process is roughly like this , Next paste sqlalchemy when commiit Source code
sqlalchemy/orm/session.py
def _prepare_impl(self):
self._assert_active()
if self._parent is None or self.nested:
self.session.dispatch.before_commit(self.session)
stx = self.session.transaction
if stx is not self:
for subtransaction in stx._iterate_self_and_parents(upto=self):
subtransaction.commit()
if not self.session._flushing:
for _flush_guard in range(100):
if self.session._is_clean(): # Judge whether the object information is changed , If there is a change, execute flush()
break
self.session.flush()
else:
raise exc.FlushError(
"Over 100 subsequent flushes have occurred within "
"session.commit() - is an after_flush() hook "
"creating new objects?"
)
if self._parent is None and self.session.twophase:
try:
for t in set(self._connections.values()):
t[1].prepare()
except:
with util.safe_reraise():
self.rollback()
self._state = PREPARED
边栏推荐
- 微信小程序手机号正则校验规则
- MySQL multi table query
- Sort method for sorting
- 【6】 Redis cache policy
- Digital collections "chaos", 100 billion market change is coming?
- 小程序搭建制作流程是怎样的?
- The project does not report an error, operates normally, and cannot request services
- ModuleNotFoundError: No module named ‘pip‘
- 小程序开发
- Flink CDC (Mysql为例)
猜你喜欢
随机推荐
Continuous login problem
ModuleNotFoundError: No module named ‘pip‘
字节Android岗4轮面试,收到 50k*18 Offer,裁员风口下成功破局
小程序开发流程详细是什么呢?
Digital collections strengthen reality with emptiness, enabling the development of the real economy
Marsnft: how do individuals distribute digital collections?
【3】 Redis features and functions
将项目部署到GPU上,并且运行
【一】redis简介
分布式集群架构场景优化解决方案:分布式ID解决方案
Pytorch deep learning single card training and multi card training
Flink CDC (MySQL as an example)
Digital collections "chaos", 100 billion market change is coming?
自动定时备份远程mysql脚本
raise RuntimeError(‘DataLoader worker (pid(s) {}) exited unexpectedly‘.format(pids_str))RuntimeErro
高端大气的小程序开发设计有哪些注意点?
UNL-类图
用于排序的sort方法
【1】 Introduction to redis
速查表之转MD5








