当前位置:网站首页>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
边栏推荐
猜你喜欢

The project does not report an error, operates normally, and cannot request services

How digital library realizes Web3.0 social networking

Regular verification rules of wechat applet mobile number

Hit your face ins? Mars digital collection platform explores digital collection light social networking

1: Why should databases be divided into databases and tables

使用pyhon封装一个定时发送邮件的工具类

DataX installation and use

Digital collections become a new hot spot in tourism industry

知识点21-泛型

微信小程序开发费用制作费用是多少?
随机推荐
mysql5.6(根据.ibd,.frm文件)恢复单表数据
matplotlib数据可视化
服务可靠性保障-watchdog
【6】 Redis cache policy
分布式集群架构场景优化解决方案:Session共享问题
DataX installation and use
使用pyhon封装一个定时发送邮件的工具类
【1】 Introduction to redis
分布式集群架构场景优化解决方案:分布式调度问题
区分实时数据、离线数据、流式数据以及批量数据的区别
raise RuntimeError(‘DataLoader worker (pid(s) {}) exited unexpectedly‘.format(pids_str))RuntimeErro
自动定时备份远程mysql脚本
小程序搭建制作流程是怎样的?
2:为什么要读写分离
记录下在线扩容服务器遇到的问题 NOCHANGE: partition 1 is size 419428319. it cannot be grown
Flink CDC (MySQL as an example)
Installation and use of flinkx
xml解析实体工具类
简单理解一下MVC和三层架构
Invalid packaging for parent POM x, must be “pom“ but is “jar“ @