{"id":786,"date":"2023-10-26T15:27:58","date_gmt":"2023-10-26T07:27:58","guid":{"rendered":"https:\/\/chenbing.name\/blog\/?p=786"},"modified":"2024-02-19T13:09:30","modified_gmt":"2024-02-19T05:09:30","slug":"%e6%85%8e%e7%94%a8sqlalchemy%e7%9a%84orm%e8%87%aa%e5%8a%a8%e6%98%a0%e5%b0%84","status":"publish","type":"post","link":"https:\/\/chenbing.name\/blog\/786\/","title":{"rendered":"\u614e\u7528sqlalchemy\u7684ORM\u81ea\u52a8\u6620\u5c04"},"content":{"rendered":"\n<p>\u535a\u4e3b\u5728\u5f00\u53d1\u4e00\u4e2a\u4f01\u4e1a\u5185\u90e8\u7cfb\u7edf\u65f6\u5019\uff0c\u4e3a\u4e86\u56fe\u65b9\u4fbf\uff0c\u76f4\u63a5\u4f7f\u7528sqlalchemy\u7684\u81ea\u52a8\u6620\u5c04\u529f\u80fd\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>def __init__(self) -> None:<br># \u521b\u5efa\u5f15\u64ce<br>self.engine = create_engine(self.DB_CONNECT_STRING, echo=True)<br># \u81ea\u52a8\u6620\u5c04<br>self.Base = automap_base()<br>self.Base.prepare(self.engine,reflect=True)<br># \u83b7\u53d6\u6240\u6709\u8868\u7684\u6620\u5c04\u7c7b<br>self.tables = self.Base.classes.keys()<\/p>\n<\/blockquote>\n\n\n\n<p>\u7ed3\u679c\u540e\u9762\u65e0\u610f\u4e2d\u53d1\u73b0\uff0c\u88ab\u6620\u5c04\u7684\u7cfb\u7edf\u7684mysql\uff0c\u5728\u4fee\u6539\u6570\u636e\u7684\u65f6\u5019\uff0c\u4f1a\u51fa\u73b0mysql\u5361\u4f4f\u7684\u73b0\u8c61\u3002<\/p>\n\n\n\n<p>\u800c\u4e14\u67e5\u8be2\u6570\u636e\u7684\u65f6\u5019\uff0c\u5f88\u5927\u6982\u7387\u8fd8\u4f1a\u5bfc\u81f4\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\u3002\u540c\u65f6\u81ea\u52a8\u6620\u5c04\u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u8868\u591a\u7684\u8bdd\uff0c\u4e5f\u5f88\u6162\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u6539\u6210\u4e86sqlalchemy\u76f4\u63a5\u4f7f\u7528\u539f\u751fsql\u65b9\u5f0f\u6765\u5b8c\u6210\u76f8\u5173\u5de5\u4f5c\uff0c\u6574\u4e2a\u4e00\u4e0b\u5c31\u8f7b\u677e\u4e86\u3002<\/p>\n\n\n\n<p>\u7279\u6b64\u8bb0\u5f55\u4e00\u4e0b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-small-font-size\">sql = text(&#8220;&#8221;&#8221;SELECT u.user_name, c.*<br>FROM modeling_comment_86 c<br>JOIN user_basic_info u ON c.user_id = u.user_id<br>WHERE c.data_id IN :issue_ids<br>ORDER BY comment_id DESC&#8221;&#8221;&#8221;).bindparams(issue_ids=issue_ids)<\/p>\n\n\n\n<p class=\"has-small-font-size\">issue_comment_query = session.execute(sql)<\/p>\n\n\n\n<p class=\"has-small-font-size\">for comment_item in issue_comment_query:<br>print(&#8220;comment_item:&#8221;, comment_item)<br>issue_item = {<br>&#8220;comment_id&#8221;: comment_item.comment_id,<br>&#8220;data_id&#8221;: comment_item.data_id,<br>&#8220;parent_id&#8221;: comment_item.parent_id,<br>&#8220;blockquote&#8221;: comment_item.blockquote,<br>&#8220;content&#8221;: comment_item.content,<br>&#8220;user_id&#8221;: comment_item.user_id,<br>&#8220;user_name&#8221;: comment_item.user_name,<br>&#8220;created_at&#8221;: str(comment_item.created_at),<br>}<br>comment_list.append(issue_item)<\/p>\n<\/blockquote>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u535a\u4e3b\u5728\u5f00\u53d1\u4e00\u4e2a\u4f01\u4e1a\u5185\u90e8\u7cfb\u7edf\u65f6\u5019\uff0c\u4e3a\u4e86\u56fe\u65b9\u4fbf\uff0c\u76f4\u63a5\u4f7f\u7528sqlalchemy\u7684\u81ea\u52a8\u6620\u5c04\u529f\u80fd\uff1a def __init__(self) -> None:# \u521b\u5efa\u5f15\u64ceself.engine = create_engine(self.DB_CONNECT_STRING, echo=True)# \u81ea\u52a8\u6620\u5c04self.Base = automap_base()self.Bas&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[86],"tags":[110,109],"class_list":["post-786","post","type-post","status-publish","format-standard","hentry","category-python","tag-orm","tag-sqlalchemy"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/chenbing.name\/blog\/wp-json\/wp\/v2\/posts\/786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chenbing.name\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chenbing.name\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chenbing.name\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chenbing.name\/blog\/wp-json\/wp\/v2\/comments?post=786"}],"version-history":[{"count":1,"href":"https:\/\/chenbing.name\/blog\/wp-json\/wp\/v2\/posts\/786\/revisions"}],"predecessor-version":[{"id":787,"href":"https:\/\/chenbing.name\/blog\/wp-json\/wp\/v2\/posts\/786\/revisions\/787"}],"wp:attachment":[{"href":"https:\/\/chenbing.name\/blog\/wp-json\/wp\/v2\/media?parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chenbing.name\/blog\/wp-json\/wp\/v2\/categories?post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chenbing.name\/blog\/wp-json\/wp\/v2\/tags?post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}