From f1097788f12ad8b299f02cfabe570f0f0f696867 Mon Sep 17 00:00:00 2001 From: moyechen Date: Fri, 8 Dec 2023 16:48:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=AC=94=E8=AE=B0=E4=BA=8E?= =?UTF-8?q?=202023-12-08=5F16:48:45?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\351\227\256\351\242\230.md" | 72 ++++++++++++++++++ python/vx.json | 10 +++ vx_notebook/notebook.db | Bin 24576 -> 24576 bytes 3 files changed, 82 insertions(+) create mode 100644 "python/python\345\217\257\345\217\230\345\257\271\350\261\241\345\234\250sqlalchemy\344\270\255\347\232\204\351\227\256\351\242\230.md" diff --git "a/python/python\345\217\257\345\217\230\345\257\271\350\261\241\345\234\250sqlalchemy\344\270\255\347\232\204\351\227\256\351\242\230.md" "b/python/python\345\217\257\345\217\230\345\257\271\350\261\241\345\234\250sqlalchemy\344\270\255\347\232\204\351\227\256\351\242\230.md" new file mode 100644 index 0000000..94f99db --- /dev/null +++ "b/python/python\345\217\257\345\217\230\345\257\271\350\261\241\345\234\250sqlalchemy\344\270\255\347\232\204\351\227\256\351\242\230.md" @@ -0,0 +1,72 @@ +# python可变对象在sqlalchemy中的问题 + + +## 问题 +sqlalchemy 在处理可变对象时,如果可变对象地址没有变化,不会触发更新 + + +```python + +repo = session_maker + +async def test_edit_fields( + self, engine,init_app_page_extractions +): + repo = container.repo() + async with repo.new_session() as session, session.begin(): + result = await repo.session.execute(select(PageCommandOrm)) + pages = result.scalars().all() + for row in pages: + print(row.edit_fields) + row.edit_fields["ff"] = 1 + + + repo.session.add_all(pages) + + async with repo.new_session() as session, session.begin(): + session.flush() + result = await repo.session.execute(select(PageCommandOrm)) + pages = result.scalars().all() + for row in pages: + print(row.edit_fields) + +``` + +输出如下 +``` +{} +{} +{} +{} +{} +{} +``` + +在上面的例子中,`row.edit_fields["ff"] = 1` 这样的操作不会修改edit_fields 这个变量的地址,因此sqlalchemy认为未修改该字段,不会更新该字段 + +而`row.edit_fields = {"ff":1}` 修改了,触发了更新 + + +## 解决方案 + +使用官方提供的方法`flag_modified` + +``` +from sqlalchemy.orm.attributes import flag_modified + +async def test_edit_fields( + self, engine, init_app_page_extractions +): + + repo = container.repo() + async with repo.new_session() as session, session.begin(): + result = await repo.session.execute(select(PageCommandOrm)) + pages = result.scalars().all() + for row in pages: + print(row.edit_fields) + row.edit_fields["ff"] = 1 + flag_modified(row, "edit_fields") # 标记该字段需更新 + + + repo.session.add_all(pages) +``` \ No newline at end of file diff --git a/python/vx.json b/python/vx.json index 833a7ee..75f24e2 100644 --- a/python/vx.json +++ b/python/vx.json @@ -160,6 +160,16 @@ "signature": "207324052537419", "tags": [ ] + }, + { + "attachment_folder": "", + "created_time": "2023-12-08T07:18:13Z", + "id": "181", + "modified_time": "2023-12-08T08:48:35Z", + "name": "python可变对象在sqlalchemy中的问题.md", + "signature": "207324068365109", + "tags": [ + ] } ], "folders": [ diff --git a/vx_notebook/notebook.db b/vx_notebook/notebook.db index 15e568a396e72c43511418eb6d91c0e6b62705a5..2c5fee6ef9b7b70faacc12fff2f43d3b7309f61a 100644 GIT binary patch delta 103 zcmV-t0GR)PzyW~30gxL3$&nmG0m-poq)!8F0t1r=P}u?k1F?Z20Y5x}H3a}^1_5w+ zbZBpG<&UrBkC^4Jx#+Q><(#N