{"id":85694,"date":"2023-12-22T23:15:31","date_gmt":"2023-12-22T15:15:31","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=85694"},"modified":"2023-12-22T23:15:31","modified_gmt":"2023-12-22T15:15:31","slug":"django-sqlalchemy-python","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/django-sqlalchemy-python.html","title":{"rendered":"Django vs SQLAlchemy\uff1a\u54ea\u4e2a Python ORM \u66f4\u597d"},"content":{"rendered":"
\u4ec0\u4e48\u662f ORM\uff1f<\/strong><\/div>\n

\u5728\u4ecb\u7ecd Python \u7684 ORM \u6846\u67b6\uff08Django \u548c SQLAlchemy\uff09\u4e0d\u540c\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u8981\u786e\u4fdd\u5b8c\u5168\u7406\u89e3 ORM \u6846\u67b6\u7684\u7528\u9014\u3002<\/p>\n

ORM \u4ee3\u8868\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff08Object Relational Mapping\uff09\u3002\u8ba9\u6211\u4eec\u4f9d\u6b21\u770b\u770b\u8fd9\u4e09\u4e2a\u5355\u8bcd\uff0c\u5b83\u4eec\u6b63\u597d\u89e3\u91ca\u4e86 ORM\u5728\u771f\u5b9e\u73af\u5883\u4e2d\u7684\u7528\u5904:<\/p>\n

\u25cf\u00a0\u5bf9\u8c61<\/strong>\u00a0\u2013 \u8fd9\u90e8\u5206\u8868\u793a\u4f7f\u7528\u6846\u67b6\u7684\u5bf9\u8c61\u548c\u7f16\u7a0b\u8bed\u8a00\uff0c\u4f8b\u5982 Python\u3002<\/p>\n

\u25cf\u00a0\u5173\u7cfb<\/strong>\u00a0\u2013 \u8fd9\u90e8\u5206\u8868\u793a\u6b63\u5728\u4f7f\u7528\u7684 RDBMS (\u5173\u7cfb\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf)\u6570\u636e\u5e93\u3002\u5176\u4e2d\u5305\u62ec\u8bb8\u591a\u6d41\u884c\u7684\u5173\u7cfb\u6570\u636e\u5e93\uff0c\u800c\u4f60\u53ef\u80fd\u6b63\u5728\u4f7f\u7528\u4ee5\u4e0b\u6570\u636e\u5e93 \u2014 MSSQL\u3001MySQL\u3001Oracle\u6570\u636e\u5e93\u3001PostgreSQL\u3001MariaDB\u3001PerconaDB\u3001TokuDB\u3002\u5927\u591a\u6570\u5173\u7cfb\u6570\u636e\u5e93\u4e4b\u95f4\u7684\u5171\u540c\u70b9\u662f\u5b83\u4eec\u7684\u5173\u7cfb\u7ed3\u6784(\u8868\u3001\u5217\uff0c\u952e\u3001\u7ea6\u675f\u7b49)\u3002<\/p>\n

\u25cf\u00a0\u6620\u5c04<\/strong>\u00a0\u2013 \u6700\u540e\u8fd9\u90e8\u5206\u8868\u793a\u524d\u4e24\u90e8\u5206\u5bf9\u8c61\u548c\u6570\u636e\u8868\u4e4b\u95f4\u7684\u6865\u6881\u548c\u8fde\u63a5\u3002<\/p>\n

\u56e0\u6b64\u53ef\u4ee5\u5f97\u51fa\u7684\u7ed3\u8bba\u662f ORM \u662f\u4e3a\u4e86\u5c06\u7f16\u7a0b\u8bed\u8a00\u4e0e\u6570\u636e\u5e93\u4e4b\u95f4\u76f8\u8fde\uff0c\u4ee5\u4fbf\u7b80\u5316\u521b\u5efa\u4f9d\u8d56\u4e8e\u6570\u636e\u7684\u5e94\u7528\u7a0b\u5e8f\u8fc7\u7a0b\u3002<\/p>\n

Django \u548c SQLAlchemy \u4e4b\u95f4\u6bd4\u8f83<\/strong><\/div>\n
\u6d3b\u52a8\u8bb0\u5f55 vs \u6570\u636e\u6620\u5c04<\/strong><\/span><\/div>\n

Django ORM<\/span>\u00a0\u91c7\u7528\u6d3b\u52a8\u8bb0\u5f55\u5b9e\u73b0 \u2014 \u5927\u591a\u6570 ORM \u4e2d\u80fd\u770b\u5230\u8fd9\u79cd\u5b9e\u73b0\u3002\u57fa\u672c\u4e0a\u4e5f\u53ef\u4ee5\u8bf4\u662f\u6570\u636e\u5e93\u4e2d\u6bcf\u4e00\u884c\u90fd\u76f4\u63a5\u6620\u5c04\u5230\u4ee3\u7801\u4e2d\u7684\u5bf9\u8c61\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002ORM \u6846\u67b6\uff08\u5982 Django\uff09 \u4e0d\u9700\u8981\u4e3a\u4e86\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5c5e\u6027\u800c\u9884\u5148\u5b9a\u4e49\u67b6\u6784\uff0c\u53ea\u9700\u8981\u4f7f\u7528\u5b83\u4eec\uff0c\u56e0\u4e3a\u6846\u67b6\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u6570\u636e\u5e93\u67b6\u6784\u201c\u7406\u89e3\u201d\u7ed3\u6784\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u53ea\u4fdd\u5b58\u8bb0\u5f55\u5230\u6570\u636e\u5e93\uff0c\u56e0\u4e3a\u5b83\u4e5f\u6620\u5c04\u5230\u8868\u4e2d\u7684\u7279\u5b9a\u884c\u3002<\/p>\n

SQLAlchemy<\/span>\u00a0\u91c7\u7528\u6570\u636e\u6620\u5c04\u5b9e\u73b0 \u2014 \u5f53\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u65f6\uff0c\u6570\u636e\u5e93\u7ed3\u6784\u548c\u5bf9\u8c61\u7ed3\u6784\u4e4b\u95f4\u5b58\u5728\u95f4\u9699\uff08\u5b83\u4eec\u4e0d\u50cf\u6d3b\u52a8\u8bb0\u5f55\u7684\u5b9e\u73b0\u662f 1:1\uff09\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5fc5\u987b\u4f7f\u7528\u53e6\u5916\u7684\u6301\u4e45\u5c42\u6765\u4fdd\u6301\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\uff08\u4f8b\u5982\u4fdd\u5b58\u5bf9\u8c61\uff09\u3002\u56e0\u6b64\u5f53\u91c7\u7528\u6d3b\u52a8\u8bb0\u5f55\u5b9e\u73b0\u7684\u65f6\u5019\u4e0d\u80fd\u53ea\u8c03\u7528 save() \u65b9\u6cd5\uff08\u53cd\u5bf9\u89c2\u70b9\uff09\uff0c\u4f46\u53e6\u4e00\u65b9\u9762\uff0c\u4ee3\u7801\u4e0d\u9700\u8981\u77e5\u9053\u6570\u636e\u5e93\u4e2d\u6574\u4e2a\u5173\u7cfb\u7ed3\u6784\u7684\u8fd0\u884c\uff0c\u56e0\u4e3a\u4ee3\u7801\u548c\u6570\u636e\u5e93\u4e4b\u95f4\u6ca1\u6709\u76f4\u63a5\u5173\u7cfb\u3002<\/p>\n

\u90a3\u4e48\u5b83\u4eec\u4e4b\u95f4\u8c01\u83b7\u80dc\u4e86\u5462\uff1f\u90fd\u6ca1\u6709\u3002\u8fd9\u53d6\u51b3\u4e8e\u4f60\u8981\u5b9e\u73b0\u4ec0\u4e48\u3002\u6211\u76f8\u4fe1\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u5927\u591a\u662f CRUD (\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u66f4\u65b0\u3001\u5220\u9664)\u7a0b\u5e8f\uff0c\u800c\u5728\u4e0d\u540c\u6570\u636e\u5b9e\u4f53\u4e4b\u95f4\u6ca1\u6709\u4f7f\u7528\u56f0\u96be\u4e14\u590d\u6742\u89c4\u5219\uff0c\u90a3\u4e48\u5e94\u8be5\u91c7\u7528\u6d3b\u52a8\u8bb0\u5f55\u5b9e\u73b0(Django)\u3002\u5b83\u5c06\u5e2e\u52a9\u4f60\u8f7b\u677e\u5feb\u901f\u5730\u4e3a\u4ea7\u54c1\u8bbe\u7f6e MVP\uff0c\u800c\u4e0d\u4f1a\u6709\u4efb\u4f55\u56f0\u96be\u3002\u5982\u679c\u6709\u8bb8\u591a\u201c\u4e1a\u52a1\u89c4\u5219\u201d\u548c\u9650\u5236\u6761\u4ef6\uff0c\u6700\u597d\u91c7\u7528\u6570\u636e\u6620\u5c04\u6a21\u578b\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u6346\u7ed1\u5e76\u5f3a\u8feb\u4e25\u683c\u9075\u7167\u6d3b\u52a8\u8bb0\u5f55\u6765\u8003\u91cf\u3002<\/p>\n

\u4f7f\u7528\u590d\u6742\u67e5\u8be2<\/strong><\/span><\/div>\n

\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cDjango \u548c SQLAlchemy \u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u3002\u73b0\u5b9e\u73af\u5883\u4e2d\u6211\u591a\u6b21\u89c1\u5230\u4e3b\u7528\u4f8b\u662f Django \u7528\u4e8e\u6240\u6709\u5e38\u89c4 CRUD \u64cd\u4f5c\uff0c\u800c SQLAlchemy \u7528\u4e8e\u66f4\u590d\u6742\u7684\u67e5\u8be2\uff0c\u901a\u5e38\u662f\u53ea\u8bfb\u67e5\u8be2\u3002<\/p>\n

\u6709\u5173\u8fd9\u65b9\u9762\u66f4\u591a\u7684\u4fe1\u606f\u548c\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u770b\u770b\u00a0BetterWorks \u5de5\u7a0b\u535a\u5ba2<\/a>(\u6211\u4eec\u6ca1\u6709\u4efb\u4f55\u8054\u7cfb\uff0c\u4f46\u4e0d\u7ba1\u600e\u6837\uff0c\u6211\u4eec\u559c\u6b22\u4ed6\u4eec\u7684\u535a\u5ba2)\u3002<\/p>\n

\u4e3b\u952e\u81ea\u52a8\u751f\u6210<\/strong><\/span><\/div>\n

\u4e24\u4e2a\u6846\u67b6\u4e4b\u95f4\u7684\u53e6\u4e00\u4e2a\u4e0d\u540c\u662f Django \u80fd\u4e3a\u8868\u81ea\u52a8\u521b\u5efa\u4e3b\u952e, SQLAlchemy \u5374\u505a\u4e0d\u5230\u3002\u5fc5\u987b\u624b\u52a8\u4e3a\u6bcf\u5f20\u8868\u521b\u5efa\u4e3b\u952e\u3002\u6743\u8861\u5229\u5f0a \u2014 \u4f60\u8ba4\u4e3a\u54ea\u79cd\u6846\u67b6\u6700\u6e05\u695a\u7b26\u5408\u8868\u7684\u4e3b\u952e\uff1f\u6839\u636e\u56e2\u961f\u7684\u77e5\u8bc6\u548c\u7ecf\u9a8c\uff0c\u53ef\u4ee5\u81ea\u884c\u51b3\u5b9a\u3002<\/p>\n

\u81ea\u52a8\u63d0\u4ea4<\/strong><\/span><\/div>\n

\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cDjango \u4f1a\u81ea\u52a8\u63d0\u4ea4\uff0c SQLAlchemy \u5374\u4e0d\u884c\u3002\u81ea\u52a8\u63d0\u4ea4\u4f1a\u5f71\u54cd\u4f7f\u7528\u6846\u67b6\u7684\u65b9\u5f0f\uff08\u4e8b\u52a1\u3001\u56de\u6eda\u7b49\uff09\u3002<\/p>\n

\u652f\u6301\u7684\u6570\u636e\u5e93<\/strong><\/span><\/div>\n

Django \u548c SQLAlchemy \u90fd\u80fd\u7528\u4e8e MySQL\u3001PostgreSQL\u3001Oracle \u548c SQLite\u3002\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528 MSSQL\uff0c\u5219\u5e94\u8be5\u4f7f\u7528 SQLAlchemy\uff0c\u56e0\u4e3a\u5b83\u5b8c\u5168\u652f\u6301 MSSQL \uff0c\u5e76\u4e14\u4e5f\u53ef\u4ee5\u627e\u5230\u66f4\u591a\u76f8\u5173\u7684\u4fe1\u606f\u548c\u6587\u6863\u3002<\/p>\n

\u5b66\u4e60\u66f2\u7ebf<\/strong><\/span><\/div>\n

\u5728\u7f51\u4e0a\u6709\u4e00\u4e2a\u666e\u904d\u7684\u89c2\u70b9\uff0c\u8ba4\u4e3a Django \u66f4\u5bb9\u6613\u5b66\u4e60\u3002\u8fd9\u662f\u663e\u800c\u6613\u89c1\u7684\uff0c\u7531\u4e8e\u5b83\u901a\u5e38\u90fd\u7528\u5728\u6ca1\u6709\u7279\u522b\u590d\u6742\u7684\u7528\u4f8b\u4e0a\u3002\u56e0\u6b64\uff0c\u5e94\u8be5\u8003\u8651\u613f\u610f\u6295\u5165\u591a\u5c11\u7cbe\u529b\u6765\u5b66\u4e60\u6846\u67b6\uff0c\u4e0e SQLAlchemy \u4ea4\u53c9\u5b66\u4e60\u4ee5\u4fbf\u83b7\u5f97\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff08\u5047\u4f7f\u4f60\u771f\u7684\u9700\u8981\u5b83\uff09\u3002<\/p>\n

\u793e\u533a\u89c4\u6a21<\/strong><\/span><\/div>\n

\u6beb\u65e0\u7591\u95ee\uff0c\u5728 Python ORM \u6846\u67b6\u4e2d SQLAlchemy \u62e5\u6709\u6700\u5927\u7684\u793e\u533a\u3002\u5982\u679c\u793e\u533a\u5bf9\u4f60\u81f3\u5173\u91cd\u8981\uff08\u6211\u8ba4\u4e3a\u5b83\u5e94\u8be5\u662f\uff09\uff0cSQLAlchemy \u8be5\u662f\u4f60\u7684\u9009\u62e9\u3002\u8fd9\u5e76\u4e0d\u8bf4\u660e\u5bf9\u4e8e\u5176\u5b83\u6846\u67b6\uff0c\u4f60\u4e0d\u80fd\u627e\u5230\u4efb\u4f55\u5e2e\u52a9\uff0c\u4f8b\u5982 Django\u3002\u4f60\u4e5f\u53ef\u4ee5\u83b7\u5f97 bug \u4fee\u590d\uff0c\u4ece StackOverflow \u5f97\u5230\u95ee\u9898\u7684\u7b54\u6848\u548c\u5176\u5b83\u9700\u8981\u7684\u5e2e\u52a9\uff0c\u4f46\u6982\u7387\u4ec5\u4ec5\u6bd4 SQLAlchemy \u9ad8\u3002<\/p>\n

\u6027\u80fd<\/strong><\/span><\/div>\n

\u6211\u8ba4\u4e3a\u53ea\u5728\u8fd9\u91cc\u5199\uff08X \u6bd4 Y \u5feb\uff09\u662f\u4e0d\u8d1f\u8d23\u4efb\u7684\u3002\u7531\u4e8e ORM \u5177\u6709\u5982\u6b64\u591a\u7279\u5f81\u548c\u529f\u80fd\uff0c\u5e76\u4e14\u5b83\u4eec\u5728\u6bcf\u4e2a\u6846\u67b6\u4e2d\u4e5f\u4e0d\u540c\uff0c\u8fd9\u5c06\u5f88\u96be\u5f97\u51fa\u7ed3\u8bba\u3002\u6839\u636e\u6211\u7684\u7ecf\u9a8c\uff0c\u4f7f\u7528\u6846\u67b6\u7279\u6027\u7684\u65b9\u5f0f\uff0c\u4f1a\u5bf9\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6570\u636e\u5c42\u7684\u6574\u4f53\u6027\u80fd\u4ea7\u751f\u6781\u5927\u5f71\u54cd\u3002\u56e0\u6b64\u6211\u5efa\u8bae\u4e0d\u8981\u901a\u8fc7\u6027\u80fd\u6765\u9009\u62e9\u6846\u67b6\uff0c\u800c\u662f\u5e94\u8be5\u5b66\u4e60\u5982\u4f55\u5408\u7406\u5229\u7528\u6846\u67b6\u3002<\/p>\n

\u5047\u5982\u5728 ORM \u6846\u67b6\u4e2d\u4f7f\u7528\u539f\u59cb\u7684 SQL \u67e5\u8be2\u3001\u4f7f\u7528 Jooq \u6216\u8005\u53ea\u662f\u90e8\u5206\u67e5\u8be2\u4e0d\u4f7f\u7528 ORM\uff0c\u53ef\u4ee5\u4e86\u89e3\u00a0EverSQL \u67e5\u8be2\u4f18\u5316\u5668<\/a>\uff0c\u8fd9\u53ef\u80fd\u662f\u6700\u7b80\u5355\u4f18\u5316\u4efb\u4f55\u67e5\u8be2\u7684\u65b9\u6cd5\u3002<\/p>\n

\u603b\u7ed3<\/strong><\/span><\/div>\n

\u4efb\u4f55\u6bd4\u8f83\u4e2d\uff0c\u6211\u8ba4\u4e3a\u6700\u597d\u628a\u51b3\u7b56\u6743\u4ea4\u8fd8\u7ed9\u8bfb\u8005\u3002\u6bcf\u4e2a\u7528\u4f8b\u4e4b\u95f4\u662f\u4e0d\u540c\u7684\uff0c\u4e0d\u540c\u7684\u6280\u672f\u4e5f\u53ef\u4ee5\u66f4\u9002\u7528\u3002\u770b\u770b\u4e0a\u9762\u6307\u51fa\u7684\u5dee\u5f02\uff0c\u8ba9\u6211\u4eec\u77e5\u9053\u4f60\u505a\u51fa\u4e86\u4ec0\u4e48\u51b3\u5b9a\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/python.jobbole.com\/88672\/<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/django-sqlalchemy-python.html<\/a>\u7f16\u8f91\uff1a\u9004\u589e\u5b9d\uff0c\u5ba1\u6838\u5458\uff1a\u5218\u9044<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u4ec0\u4e48\u662f ORM\uff1f \u5728\u4ecb\u7ecd Python \u7684 ORM \u6846\u67b6\uff08Django \u548c SQLAlchemy\uff09\u4e0d\u540c\u4e4b\u524d\uff0c\u6211 […]<\/p>\n","protected":false},"author":309,"featured_media":85698,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-85694","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/85694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=85694"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/85694\/revisions"}],"predecessor-version":[{"id":85713,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/85694\/revisions\/85713"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/85698"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=85694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=85694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=85694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}