当前位置:网站首页>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
边栏推荐
- There is a problem with MySQL paging
- Idempotent component
- Flume安装及使用
- Chapter 8 aggregate function
- 分布式锁-数据库实现
- CertPathValidatorException:validity check failed
- Addition, deletion and modification of data processing; constraint
- 1: Why should databases be divided into databases and tables
- 使用神经网络实现对天气的预测
- SQLAlchemy使用相关
猜你喜欢

Pytorch deep learning single card training and multi card training

Data warehouse tool superset installation (fully available)

【二】redis基础命令与使用场景

服务可靠性保障-watchdog

Regular verification rules of wechat applet mobile number

项目不报错,正常运行,无法请求到服务

Create a virtual environment using pycharm

self-attention学习笔记

Mars number * word * Tibet * product * Pingtai defender plan details announced

Dataset class loads datasets in batches
随机推荐
分布式锁-数据库实现
小程序开发流程详细是什么呢?
Idempotent component
使用神经网络实现对天气的预测
Phoenix
uniapp webview监听页面加载后回调
mysql分页出现问题
4个角度教你选小程序开发工具?
Wechat official account - authorized login
Hit your face ins? Mars digital collection platform explores digital collection light social networking
Books - Sun Tzu's art of war
Assembly packaging
【3】 Redis features and functions
On July 7, the national wind 24 solar terms "Xiaoshu" came!! Attachment.. cooperation.. completion.. advance.. report
The project does not report an error, operates normally, and cannot request services
1:为什么数据库要分库分表
JS!!
微信小程序开发费用制作费用是多少?
NLP中常用的utils
Data warehouse tool superset installation (fully available)