{"id":2853,"date":"2023-08-28T09:09:46","date_gmt":"2023-08-28T00:09:46","guid":{"rendered":"https:\/\/edu.ujhb.org\/?p=2853"},"modified":"2023-08-28T09:09:46","modified_gmt":"2023-08-28T00:09:46","slug":"sqlalchemy%e5%85%a5%e9%96%80%ef%bc%88%e6%97%a5%e6%9c%ac%e8%aa%9e%e7%89%88%ef%bc%89","status":"publish","type":"post","link":"https:\/\/edu.ujhb.org\/?p=2853","title":{"rendered":"SqlAlchemy\u5165\u9580\uff08\u65e5\u672c\u8a9e\u7248\uff09"},"content":{"rendered":"\n<p>\u30a6\u30a7\u30d6\u30b5\u30a4\u30c8\u3078\u306e\u30ea\u30f3\u30af\u3092\u305f\u3069\u3063\u3066\u3001SQL\u932c\u91d1\u8853\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306e\u6982\u8981\u3092\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044 https:\/\/github.com\/sqlalchemy\/sqlalchemyChatGPT<\/p>\n\n\n\n<p>\u4e2d\u53e4<strong>\u30df\u30ad\u30b5\u30fc\u30dc\u30c3\u30af\u30b9\u30a6\u30a7\u30d6\u691c\u7d22G<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">SQL \u5316\u5b66\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306e\u6982\u8981<\/h4>\n\n\n\n<p>GitHub \u30ea\u30dd\u30b8\u30c8\u30ea\u306b\u57fa\u3065\u304f\u3068\u3001SQLAlchemy \u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306e\u6982\u8981\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7c21\u5358\u306a\u7d39\u4ecb<\/strong>\n<ul class=\"wp-block-list\">\n<li>SQLAlchemy\u3068\u306f\u4f55\u3067\u3059\u304b?<\/li>\n\n\n\n<li>\u4e3b\u306a\u6a5f\u80fd\u3068\u5229\u70b9<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3068\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7<\/strong>\n<ul class=\"wp-block-list\">\n<li>SQLAlchemy \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u65b9\u6cd5<\/li>\n\n\n\n<li>\u8a2d\u5b9a\u3092\u521d\u671f\u5316\u3059\u308b<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u57fa\u672c\u30b3\u30f3\u30bb\u30d7\u30c8<\/strong>\n<ul class=\"wp-block-list\">\n<li>ORM (\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 \u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb \u30de\u30c3\u30d4\u30f3\u30b0)<\/li>\n\n\n\n<li>\u30b3\u30a2 SQL \u30d3\u30eb\u30c9 \u30b7\u30b9\u30c6\u30e0<\/li>\n\n\n\n<li>DBAPI \u30a4\u30f3\u30bf\u30e9\u30af\u30b7\u30e7\u30f3 \u30ec\u30a4\u30e4\u30fc<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30e2\u30c7\u30eb\u306e\u4f5c\u6210\u3068\u7ba1\u7406<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b<\/li>\n\n\n\n<li>\u30c6\u30fc\u30d6\u30eb\u306e\u4f5c\u6210\u3068\u524a\u9664<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30b7\u30b9\u30c6\u30e0\u3092\u7167\u4f1a\u3059\u308b<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u57fa\u672c\u30af\u30a8\u30ea<\/li>\n\n\n\n<li>\u8907\u96d1\u306a\u30af\u30a8\u30ea\u3068\u30b5\u30d6\u30af\u30a8\u30ea<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3068\u7d50\u5408<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5<\/li>\n\n\n\n<li>\u7d50\u5408\u64cd\u4f5c\u306e\u5b9f\u884c\u65b9\u6cd5<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u958b\u59cb\u304a\u3088\u3073\u7d42\u4e86\u3059\u308b\u65b9\u6cd5<\/li>\n\n\n\n<li>\u30a8\u30e9\u30fc\u51e6\u7406\u3068\u30ed\u30fc\u30eb\u30d0\u30c3\u30af<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u6700\u9069\u5316<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u71b1\u5fc3\u306a\u8ca0\u8377<\/li>\n\n\n\n<li>\u30af\u30a8\u30ea\u306e\u6700\u9069\u5316<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30a4\u30f3\u30c8\u30ed\u30b9\u30da\u30af\u30b7\u30e7\u30f3\u3068\u751f\u6210<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30ad\u30fc\u30de\u306e\u53cd\u6620\u65b9\u6cd5<\/li>\n\n\n\n<li>CREATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u81ea\u52d5\u7684\u306b\u751f\u6210\u3059\u308b<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u30d9\u30b9\u30c8 \u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3068\u9ad8\u5ea6\u306a\u30c8\u30d4\u30c3\u30af<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u5b89\u5168<\/li>\n\n\n\n<li>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>\u3053\u306e\u6982\u8981\u306f\u3001\u7279\u306b\u65b0\u3057\u3044 IT \u5b66\u7fd2\u8005\u3084\u72ec\u81ea\u306e\u7ba1\u7406\u30b7\u30b9\u30c6\u30e0\u306e\u958b\u767a\u3092\u691c\u8a0e\u3057\u3066\u3044\u308b\u4f01\u696d\u306b\u3068\u3063\u3066\u3001SQLAlchemy \u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u57fa\u790e\u3068\u3057\u3066\u5f79\u7acb\u3061\u307e\u3059\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/www.mixerbox.com\/chatgpt-plugins?utm_source=chatgpt_us&amp;utm_medium=chatgptresponse_us&amp;utm_campaign=crosspromotion_us_20230818\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.mixerbox.com\/chatgpt-plugins?utm_source=chatgpt_us&amp;utm_medium=chatgptresponse_us&amp;utm_campaign=crosspromotion_us_20230818\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/user-images.strikinglycdn.com\/res\/hrscywv4p\/image\/upload\/c_limit,fl_lossy,h_630,w_1200,f_auto,q_auto\/55375\/391472_893752.png\" alt=\"image of MixerBox ChatGPT Plugins\"\/><\/a><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.mixerbox.com\/chatgpt-plugins?utm_source=chatgpt_us&amp;utm_medium=chatgptresponse_us&amp;utm_campaign=crosspromotion_us_20230818\" target=\"_blank\" rel=\"noreferrer noopener\">\u30df\u30ad\u30b5\u30fc\u30dc\u30c3\u30af\u30b9\u30c1\u30e3\u30c3\u30c8GPT\u30d7\u30e9\u30b0\u30a4\u30f3<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.mixerbox.com\/chatgpt-plugins?utm_source=chatgpt_us&amp;utm_medium=chatgptresponse_us&amp;utm_campaign=crosspromotion_us_20230818\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/static.mixerbox.com\/chatai\/chatgpt-plugin\/WebSearchG_logo.png\" alt=\"mixerbox.com logo\"\/><\/a><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.mixerbox.com\/chatgpt-plugins?utm_source=chatgpt_us&amp;utm_medium=chatgptresponse_us&amp;utm_campaign=crosspromotion_us_20230818\" target=\"_blank\" rel=\"noreferrer noopener\">mixerbox.com<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>SQLAlchemy\u3068\u306f\u4f55\u304b\u3092\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044\u3002 \u30c1\u30e3\u30c3\u30c8GPT<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SQLAlchemy\u3068\u306f\u4f55\u3067\u3059\u304b?<\/h3>\n\n\n\n<p>SQLAlchemy \u306f\u3001Python \u7528\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c4\u30fc\u30eb\u30ad\u30c3\u30c8\u304a\u3088\u3073\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 \u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb \u30de\u30c3\u30d4\u30f3\u30b0 (ORM) \u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002 \u3053\u308c\u306b\u3088\u308a\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u8005\u306f\u3001SQL\u306e\u5b8c\u5168\u306a\u6a5f\u80fd\u3068\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u306a\u304c\u3089\u3001Python\u306e\u65b9\u6cd5\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u5bfe\u8a71\u3067\u304d\u307e\u3059\u3002 SQLAlchemy \u306f\u3001\u52b9\u7387\u7684\u3067\u9ad8\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30a2\u30af\u30bb\u30b9\u3092\u5b9f\u73fe\u3059\u308b\u4e00\u9023\u306e\u30a8\u30f3\u30bf\u30fc\u30d7\u30e9\u30a4\u30ba \u30af\u30e9\u30b9\u306e\u6c38\u7d9a\u5316\u30d1\u30bf\u30fc\u30f3\u3092\u63d0\u4f9b\u3059\u308b\u3088\u3046\u306b\u8a2d\u8a08\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u8981\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>ORM (\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 \u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb \u30de\u30c3\u30d4\u30f3\u30b0):<\/strong>&nbsp;SQLAlchemy \u306e ORM \u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u958b\u767a\u8005\u306f Python \u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3067\u304d\u307e\u3059\u3002 \u3053\u308c\u3089\u306e\u30af\u30e9\u30b9\u306f\u81ea\u7136\u306b\u69cb\u7bc9\u304a\u3088\u3073\u64cd\u4f5c\u3067\u304d\u3001\u3059\u3079\u3066\u306e\u5909\u66f4\u306f\u73fe\u5728\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068\u81ea\u52d5\u7684\u306b\u540c\u671f\u3055\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30b3\u30a2SQL\u30d3\u30eb\u30c9\u30b7\u30b9\u30c6\u30e0<\/strong>:\u3053\u308c\u306f\u3001\u5b8c\u5168\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u62bd\u8c61\u5316\u30ec\u30a4\u30e4\u30fc\u3067\u3042\u308bSQLAlchemy\u306e\u3082\u30461\u3064\u306e\u4e3b\u8981\u306a\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u3059\u3002 \u3053\u308c\u306b\u306f\u3001Python \u30d9\u30fc\u30b9\u306e SQL \u5f0f\u8a00\u8a9e\u3001\u30b9\u30ad\u30fc\u30de \u30e1\u30bf\u30c7\u30fc\u30bf\u3001\u63a5\u7d9a\u30d7\u30fc\u30eb\u3001\u578b\u306e\u9069\u7528\u3001\u304a\u3088\u3073\u30ab\u30b9\u30bf\u30e0\u578b\u304c\u542b\u307e\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>DBAPI<\/strong>&nbsp;\u5bfe\u8a71\u30ec\u30a4\u30e4\u30fc: \u3053\u306e\u30ec\u30a4\u30e4\u30fc\u306f\u3001SQL \u30af\u30a8\u30ea\u306e\u9001\u4fe1\u3084\u7d50\u679c\u306e\u53d7\u4fe1\u306a\u3069\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u306e\u4f4e\u30ec\u30d9\u30eb\u306e\u5bfe\u8a71\u3092\u51e6\u7406\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u306a\u6a5f\u80fd<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u67d4\u8edf\u306a\u30af\u30a8\u30ea \u30b7\u30b9\u30c6\u30e0<\/strong>: SQLAlchemy \u3092\u4f7f\u7528\u3059\u308b\u3068\u3001SQL \u3092\u8a18\u8ff0\u3059\u308b\u3068\u304d\u306b\u4f7f\u7528\u3059\u308b\u306e\u3068\u540c\u3058\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u7d50\u5408\u624b\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u30af\u30a8\u30ea\u3092\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002 \u30ea\u30c6\u30e9\u30eb SQL \u306f\u3044\u3064\u3067\u3082\u4f7f\u7528\u3067\u304d\u307e\u3059\u304c\u3001\u3053\u308c\u304c\u5fc5\u8981\u306b\u306a\u308b\u3053\u3068\u306f\u307b\u3068\u3093\u3069\u3042\u308a\u307e\u305b\u3093\u3002<\/li>\n\n\n\n<li><strong>\u71b1\u5fc3\u306a<\/strong>\u8aad\u307f\u8fbc\u307f:\u95a2\u9023\u3059\u308b\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8aad\u307f\u8fbc\u3080\u305f\u3081\u306e\u5305\u62ec\u7684\u3067\u67d4\u8edf\u306a\u4e00\u62ec\u8aad\u307f\u8fbc\u307f\u30b7\u30b9\u30c6\u30e0\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406<\/strong>: SQLAlchemy \u306e ORM \u3067\u306f\u3001\u547c\u3073\u51fa\u3055\u308c\u308b\u307e\u3067\u5909\u66f4\u306f\u30b9\u30c8\u30ec\u30fc\u30b8\u306b\u4fdd\u6301\u3055\u308c\u307e\u305b\u3093\u3002<code>commit()<\/code><\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30a4\u30f3\u30c8\u30ed\u30b9\u30da\u30af\u30b7\u30e7\u30f3\u3068\u751f\u6210<\/strong>:\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30ad\u30fc\u30de\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30e1\u30bf\u30c7\u30fc\u30bf\u30921\u3064\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u8868\u3059Python\u69cb\u9020\u306b\u53cd\u6620\u3067\u304d\u307e\u3059\u3002 \u3053\u308c\u3089\u306e\u69cb\u9020\u4f53\u306f\u3001CREATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u751f\u6210\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3<\/strong>:\u53ef\u80fd\u306a\u9650\u308a\u30d0\u30a4\u30f3\u30c7\u30a3\u30f3\u30b0\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u3001SQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u653b\u6483\u306f\u554f\u984c\u306b\u306a\u308a\u307e\u305b\u3093\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528\u30b7\u30ca\u30ea\u30aa<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Web \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3 \u30d0\u30c3\u30af\u30a8\u30f3\u30c9<\/li>\n\n\n\n<li>\u30c7\u30fc\u30bf\u5206\u6790\u3068\u30c7\u30fc\u30bf\u5909\u63db\u306e\u30bf\u30b9\u30af<\/li>\n\n\n\n<li>\u81ea\u52d5\u5316\u30b9\u30af\u30ea\u30d7\u30c8<\/li>\n\n\n\n<li>\u30a8\u30f3\u30bf\u30fc\u30d7\u30e9\u30a4\u30ba\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3<\/li>\n<\/ul>\n\n\n\n<p>SQLAlchemy\u306e\u54f2\u5b66\u306f\u3001SQL\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306f\u3001\u30b5\u30a4\u30ba\u3068\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u91cd\u8981\u306b\u306a\u308a\u59cb\u3081\u305f\u3068\u304d\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3057\u306a\u304f\u306a\u308b\u3053\u3068\u3067\u3059\u3002 \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306f\u3001\u62bd\u8c61\u5316\u304c\u91cd\u8981\u306b\u306a\u308a\u59cb\u3081\u308b\u3068\u3001\u30c6\u30fc\u30d6\u30eb\u3084\u884c\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3057\u306a\u304f\u306a\u308a\u307e\u3059\u3002 SQLAlchemy \u306f\u3001\u3053\u308c\u3089 2 \u3064\u306e\u539f\u5247\u306b\u5bfe\u5fdc\u3059\u308b\u3088\u3046\u306b\u8a2d\u8a08\u3055\u308c\u3066\u304a\u308a\u3001\u958b\u767a\u8005\u306f\u30c9\u30e1\u30a4\u30f3 \u30e2\u30c7\u30eb\u3068\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb \u30b9\u30ad\u30fc\u30de\u306e\u9593\u306b\u30ab\u30b9\u30bf\u30e0 \u30e1\u30c7\u30a3\u30a8\u30fc\u30b7\u30e7\u30f3 \u30ec\u30a4\u30e4\u30fc\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u3001\u67d4\u8edf\u3067\u52b9\u7387\u7684\u3067\u5b89\u5168\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30bd\u30ea\u30e5\u30fc\u30b7\u30e7\u30f3\u3092\u5fc5\u8981\u3068\u3059\u308b\u958b\u767a\u8005\u3084\u4f01\u696d\u306b\u6700\u9069\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>SQLAlchemy\u30c1\u30e3\u30c3\u30c8GPT\u306e\u4e3b\u306a\u6a5f\u80fd\u3068\u5229\u70b9\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SQL\u5316\u5b66\u306e\u4e3b\u306a\u6a5f\u80fd\u3068\u5229\u70b9<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u306a\u6a5f\u80fd<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u30de\u30c3\u30d4\u30f3\u30b0(<\/strong>ORM):SQLAlchemy\u306eORM\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001Python\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u304a\u3088\u3073\u64cd\u4f5c\u3067\u304d\u307e\u3059\u3002 \u3053\u308c\u3089\u306e\u30af\u30e9\u30b9\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30c6\u30fc\u30d6\u30eb\u306b1\u5bfe1\u3067\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u304c\u3088\u308a\u76f4\u611f\u7684\u3067Python\u7684\u306b\u306a\u308a\u307e\u3059\u3002<\/li>\n\n\n\n<li>SQL \u5f0f\u8a00\u8a9e: SQLAlchemy Core \u306f\u3001\u958b\u767a\u8005\u304c Python \u306e\u65b9\u6cd5\u3067\u8907\u96d1\u306a SQL \u30af\u30a8\u30ea\u3092\u4f5c\u6210\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u5f37\u529b\u306a SQL&nbsp;<strong>\u5f0f\u8a00\u8a9e<\/strong>\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u62bd\u8c61\u5316: SQLAlchemy \u306f\u3001DBAPI \u3092\u901a\u3058\u3066\u3001\u7570\u306a\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7ba1\u7406\u30b7\u30b9\u30c6\u30e0 (MySQL\u3001PostgreSQL\u3001SQLite \u306a\u3069) \u3092\u30b3\u30fc\u30c9\u3067\u7c21\u5358\u306b\u5207\u308a\u66ff\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9<strong>\u62bd\u8c61\u5316<\/strong>\u30ec\u30a4\u30e4\u30fc\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u4e00\u62ec\u8aad\u307f\u8fbc\u307f<\/strong>: SQLAlchemy \u306b\u306f\u3001\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u30af\u30a8\u30ea\u306b\u4e8b\u524d\u306b\u8aad\u307f\u8fbc\u3080\u3053\u3068\u3067\u30af\u30a8\u30ea\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u308b\u3001\u4e00\u62ec\u8aad\u307f\u8fbc\u307f\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u3044\u304f\u3064\u304b\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3<\/strong>\u7ba1\u7406: SQLAlchemy \u306f\u3001\u30cd\u30b9\u30c8\u3055\u308c\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3084\u30bb\u30fc\u30d6\u30dd\u30a4\u30f3\u30c8\u306a\u3069\u306e\u8907\u96d1\u306a\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u30a8\u30e9\u30fc\u51e6\u7406\u3068\u30c7\u30fc\u30bf\u306e\u4e00\u8cab\u6027\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf \u30bb\u30ad\u30e5\u30ea\u30c6\u30a3<\/strong>: SQLAlchemy \u306f\u3001\u30d0\u30a4\u30f3\u30c9 \u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3068\u81ea\u52d5\u30a8\u30b9\u30b1\u30fc\u30d7\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001SQL \u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u653b\u6483\u306e\u9632\u6b62\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u67d4\u8edf\u306a\u30af\u30a8\u30ea\u69cb\u7bc9<\/strong>: \u3055\u307e\u3056\u307e\u306a\u8907\u96d1\u3055\u306e\u30af\u30a8\u30ea\u306b\u5bfe\u5fdc\u3059\u308b\u305f\u3081\u306b\u3001\u30c1\u30a7\u30fc\u30f3 \u30e1\u30bd\u30c3\u30c9\u3084\u30cf\u30a4\u30d6\u30ea\u30c3\u30c9 \u30d7\u30ed\u30d1\u30c6\u30a3\u306a\u3069\u3001\u30af\u30a8\u30ea\u3092\u69cb\u7bc9\u3059\u308b\u3055\u307e\u3056\u307e\u306a\u65b9\u6cd5\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u79fb\u884c\u3068<strong>\u30a4\u30f3\u30c8\u30ed\u30b9\u30da\u30af\u30b7\u30e7\u30f3<\/strong>: SQLAlchemy \u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30b9\u30ad\u30fc\u30de\u3092 &#8220;\u53cd\u6620&#8221; \u3057\u3001\u5bfe\u5fdc\u3059\u308b CREATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3068 DROP \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u751f\u6210\u3067\u304d\u308b\u305f\u3081\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u79fb\u884c\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u5229<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u67d4\u8edf\u6027<\/strong>: SQLAlchemy \u306f\u3001ORM \u30ec\u30d9\u30eb\u3068 SQL \u5f0f\u8a00\u8a9e\u30ec\u30d9\u30eb\u306e\u4e21\u65b9\u3067\u975e\u5e38\u306b\u67d4\u8edf\u6027\u304c\u3042\u308a\u3001\u4e8b\u5b9f\u4e0a\u3042\u3089\u3086\u308b\u7a2e\u985e\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9<\/strong>: SQLAlchemy \u306f\u3001\u63a5\u7d9a\u30d7\u30fc\u30eb\u3084\u4e00\u62ec\u8aad\u307f\u8fbc\u307f\u306a\u3069\u306e\u3055\u307e\u3056\u307e\u306a\u6700\u9069\u5316\u3092\u901a\u3058\u3066\u3001\u9ad8\u8ca0\u8377\u306e\u4e0b\u3067\u9069\u5207\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u9593\u306e\u4e92\u63db\u6027<\/strong>: \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u62bd\u8c61\u5316\u30ec\u30a4\u30e4\u30fc\u306b\u3088\u308a\u3001SQLAlchemy \u3092\u4f7f\u7528\u3059\u308b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306f\u3001\u30b3\u30fc\u30c9\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u306a\u304f\u3001\u8907\u6570\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u30b7\u30fc\u30e0\u30ec\u30b9\u306b\u5207\u308a\u66ff\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u306e\u30b5\u30dd\u30fc\u30c8<\/strong>\u3068\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8: SQLAlchemy \u306b\u306f\u5927\u898f\u6a21\u306a\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u3068\u8c4a\u5bcc\u306a\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u304c\u3042\u308b\u305f\u3081\u3001\u554f\u984c\u306e\u89e3\u6c7a\u3068\u5b66\u7fd2\u304c\u6bd4\u8f03\u7684\u7c21\u5358\u3067\u3059\u3002<\/li>\n\n\n\n<li>\u6210\u719f\u5ea6\u3068\u5b89\u5b9a\u6027: SQLAlchemy\u306f\u9577\u5e74\u524d\u304b\u3089\u5b58\u5728\u3057\u3066\u304a\u308a\u3001\u3044\u304f\u3064\u304b\u306e\u5927\u898f\u6a21\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u5e83\u304f\u4f7f\u7528\u3055\u308c\u3066\u304a\u308a\u3001\u305d\u306e\u6210\u719f<strong>\u5ea6\u3068\u5b89\u5b9a\u6027<\/strong>\u3092\u8a3c\u660e\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30d5\u30eb<\/strong>&nbsp;\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb: \u4e00\u90e8\u306e\u81ea\u52d5\u5316\u3055\u308c\u305f ORM \u3068\u306f\u7570\u306a\u308a\u3001SQLAlchemy \u3067\u306f SQL \u30af\u30a8\u30ea\u306e\u751f\u6210\u3068\u5b9f\u884c\u3092\u5b8c\u5168\u306b\u5236\u5fa1\u3067\u304d\u308b\u305f\u3081\u3001\u30af\u30a8\u30ea\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u624b\u52d5\u3067\u6700\u9069\u5316\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306b\u4fbf\u5229\u3067\u3059\u3002<\/li>\n\n\n\n<li>\u9ad8\u5ea6\u306a\u6a5f\u80fd: \u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\u306a\u95a2\u9023\u4ed8\u3051\u3001\u30c7\u30fc\u30bf \u30de\u30c3\u30d7\u306e\u7d99\u627f\u3001\u6df7\u5408\u5c5e\u6027\u306a\u3069\u306e\u9ad8\u5ea6\u306a ORM&nbsp;<strong>\u6a5f\u80fd<\/strong>\u304c\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30c6\u30b9\u30c8\u3068\u30c7\u30d0\u30c3\u30b0: \u30af\u30a8\u30ea\u5206\u6790\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9 \u30c6\u30b9\u30c8\u3001<strong>\u304a\u3088\u3073\u30c7\u30d0\u30c3\u30b0<\/strong>\u306e\u305f\u3081\u306e\u30c4\u30fc\u30eb\u3068\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u6a5f\u80fd\u3068\u5229\u70b9\u306b\u3088\u308a\u3001SQLAlchemy \u306f\u5358\u7d14\u306a CRUD \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3060\u3051\u3067\u306a\u304f\u3001\u8907\u96d1\u306a\u30a8\u30f3\u30bf\u30fc\u30d7\u30e9\u30a4\u30ba \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30bd\u30ea\u30e5\u30fc\u30b7\u30e7\u30f3\u306b\u3082\u9069\u3057\u3066\u3044\u307e\u3059\u3002 \u3053\u308c\u306b\u3088\u308a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u306b\u95a2\u3057\u3066Python\u958b\u767a\u8005\u306b\u6700\u9069\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>SQLAlchemyChatGPT\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u3092\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SQLAlchemy \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5<\/h3>\n\n\n\n<p>SQLAlchemy \u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3068\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u306f\u3001\u6bd4\u8f03\u7684\u5358\u7d14\u3067\u7c21\u5358\u306a\u30d7\u30ed\u30bb\u30b9\u3067\u3059\u3002 \u8a73\u7d30\u306a\u624b\u9806\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u53d6\u308a\u4ed8\u3051<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>pip\u3092\u4f7f\u7528\u3057\u3066<strong>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b<\/strong>\u6700\u3082\u7c21\u5358\u306a\u65b9\u6cd5\u306f\u3001Python\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u30de\u30cd\u30fc\u30b8\u30e3\u30fcpip\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3059\u3002 \u30bf\u30fc\u30df\u30ca\u30eb\u307e\u305f\u306f\u30b3\u30de\u30f3\u30c9 \u30d7\u30ed\u30f3\u30d7\u30c8\u3092\u958b\u304d\u3001\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3059: bashCopy \u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u6700\u65b0\u30d0\u30fc\u30b8\u30e7\u30f3\u306e SQLAlchemy \u304c\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u304a\u3088\u3073\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u307e\u3059\u3002<code>pip install SQLAlchemy<\/code><\/li>\n\n\n\n<li>Anaconda \u3092\u4f7f\u7528\u3057\u3066 Python \u74b0\u5883\u3092\u7ba1\u7406\u3059\u308b<strong>\u5834\u5408\u306f<\/strong>\u3001conda \u3092\u4f7f\u7528\u3057\u3066 SQLAlchemy:bashCopy \u30b3\u30fc\u30c9\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059<code>conda install sqlalchemy<\/code><\/li>\n\n\n\n<li>\u6700\u65b0\u306e\u958b\u767a\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u5fc5\u8981\u306a\u5834\u5408\u3001\u307e\u305f\u306fSQLAlchemy\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u5909\u66f4\u3059\u308b<strong>\u5834\u5408\u306f<\/strong>\u3001GitHub\u30ea\u30dd\u30b8\u30c8\u30ea\u304b\u3089\u30af\u30ed\u30fc\u30f3\u3092\u4f5c\u6210\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u304d\u307e\u3059\u3002<code>git clone https:\/\/github.com\/sqlalchemy\/sqlalchemy.git cd sqlalchemy python setup.py install<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e26\u3079\u308b<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>SQL \u30a2\u30eb\u30b1\u30df\u30fc\u306e\u30a4\u30f3\u30dd\u30fc\u30c8 Python \u30b9\u30af\u30ea\u30d7\u30c8\u307e\u305f\u306f\u5bfe\u8a71\u578b\u74b0\u5883\u3067\u3001<strong>SQLAlchemy<\/strong>&nbsp;\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3066\u4f7f\u7528\u3092\u958b\u59cb\u3057\u307e\u3059\u3002 pythonCopy code<code>import sqlalchemy<\/code><\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30a8\u30f3\u30b8\u30f3\u3092\u4f5c\u6210\u3059\u308b<\/strong>SQLAlchemy \u306f\u3001\u30a8\u30f3\u30b8\u30f3 \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u5bfe\u8a71\u3057\u307e\u3059\u3002 pythonCopy \u30b3\u30fc\u30c9\u306f\u3001\u3053\u3053\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c0\u30a4\u30a2\u30ec\u30af\u30c8 (\u4f8b\u3048\u3070\u3001, \u306a\u3069) \u3067\u3042\u308a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u8cc7\u683c\u60c5\u5831\u3067\u3042\u308a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b5\u30fc\u30d0\u30fc\u306e\u30a2\u30c9\u30ec\u30b9\u3068\u30dd\u30fc\u30c8\u3067\u3042\u308a\u3001\u4f7f\u7528\u3059\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d\u3067\u3059\u3002<code>from sqlalchemy import create_engine engine = create_engine('dialect:\/\/username:password@host:port\/database')&nbsp;<\/code><code>dialect<\/code><code>mysql<\/code><code>postgresql<\/code><code>sqlite<\/code><code>username<\/code><code>password<\/code><code>host<\/code><code>port<\/code><code>database<\/code><\/li>\n\n\n\n<li><strong>\u30e2\u30c7\u30eb\u3068\u30c6\u30fc\u30d6\u30eb\u306e\u4f5c\u6210<\/strong>&nbsp;SQLAlchemy \u306e ORM \u6a5f\u80fd\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001Python \u30af\u30e9\u30b9\u3092\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30e2\u30c7\u30eb\u3068\u3057\u3066\u5b9a\u7fa9\u3057\u3001\u305d\u308c\u3089\u306e\u30e2\u30c7\u30eb\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002 pythonCopy code \u306f\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059: pythonCopy code<code>from sqlalchemy import Column, Integer, String from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String)&nbsp;<\/code><code>Base.metadata.create_all(engine)<\/code><\/li>\n\n\n\n<li><strong>\u30bb\u30c3\u30b7\u30e7\u30f3\u7ba1\u7406<\/strong>\u3067\u306f\u3001SQLAlchemy \u306e\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u7ba1\u7406\u3057\u307e\u3059\u3002 pythonCopy code<code>Session<\/code><code>from sqlalchemy.orm import Session session = Session(bind=engine)<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u4e0a\u8a18\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3068\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u306e\u624b\u9806\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u306b SQLAlchemy \u306e\u4f7f\u7528\u3092\u958b\u59cb\u3067\u304d\u308b\u306f\u305a\u3067\u3059\u3002 \u3053\u308c\u3089\u306f\u6700\u3082\u57fa\u672c\u7684\u306a\u624b\u9806\u3067\u3042\u308a\u3001SQLAlchemy \u306b\u306f\u3001\u7279\u5b9a\u306e\u30cb\u30fc\u30ba\u306b\u5408\u308f\u305b\u3066\u69cb\u6210\u3067\u304d\u308b\u3088\u308a\u9ad8\u5ea6\u306a\u6a5f\u80fd\u3068\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7 \u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>ChatGPT\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3068\u8a2d\u5b9a\u306e\u521d\u671f\u8a2d\u5b9a\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SQLAlchemy \u521d\u671f\u5316\u8a2d\u5b9a<\/h3>\n\n\n\n<p>\u521d\u671f\u5316\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u9069\u5207\u306b\u5bfe\u8a71\u3059\u308b\u305f\u3081\u306b SQLAlchemy \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u5f8c\u306b\u767a\u751f\u3059\u308b\u4e00\u9023\u306e\u69cb\u6210\u624b\u9806\u3067\u3059\u3002 \u901a\u5e38\u3001\u3053\u308c\u3089\u306e\u8a2d\u5b9a\u306b\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30a8\u30f3\u30b8\u30f3\u306e\u4f5c\u6210\u3001\u30c7\u30fc\u30bf \u30e2\u30c7\u30eb\u306e\u5b9a\u7fa9\u3001\u304a\u3088\u3073\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u521d\u671f\u5316\u304c\u542b\u307e\u308c\u307e\u3059\u3002 \u8a73\u7d30\u306a\u521d\u671f\u5316\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u624b\u9806\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30a8\u30f3\u30b8\u30f3\u3092\u4f5c\u6210\u3059\u308b<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5fc5\u8981\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b\u306b\u306f\u3001\u307e\u305a\u95a2\u6570<strong>\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b<\/strong>\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 pythonCopy code<code>create_engine<\/code><code>from sqlalchemy import create_engine<\/code><\/li>\n\n\n\n<li><strong>\u30a8\u30f3\u30b8\u30f3 \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u4f5c\u6210<\/strong>\u00a0\u3053\u306e\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3001\u30a8\u30f3\u30b8\u30f3 \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002 \u3053\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u306e\u3059\u3079\u3066\u306e\u5bfe\u8a71\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002 pythonCopy code<code>create_engine<\/code><code>engine = create_engine('dialect:\/\/username:password@host:port\/database')<\/code>\n<ul class=\"wp-block-list\">\n<li><code>dialect<\/code>: \u3001\u3001\u306a\u3069\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u65b9\u8a00\u3002<code>mysql<\/code><code>postgresql<\/code><code>sqlite<\/code><\/li>\n\n\n\n<li><code>username<\/code>&nbsp;\u304a\u3088\u3073 : \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30ed\u30b0\u30a4\u30f3\u8cc7\u683c\u60c5\u5831\u3002<code>password<\/code><\/li>\n\n\n\n<li><code>host<\/code>&nbsp;\u304a\u3088\u3073 : \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30b5\u30fc\u30d0\u30fc\u306e\u30a2\u30c9\u30ec\u30b9\u3068\u30dd\u30fc\u30c8\u3002<code>port<\/code><\/li>\n\n\n\n<li><code>database<\/code>: \u63a5\u7d9a\u5148\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u540d\u524d\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c7\u30fc\u30bf \u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\u5217\u3068\u30c7\u30fc\u30bf\u578b\u3092\u5b9a\u7fa9\u3059\u308b<strong>\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30dd\u30fc\u30c8<\/strong>\u3057\u307e\u3059\u3002 pythonCopy code<code>from sqlalchemy import Column, Integer, String<\/code><\/li>\n\n\n\n<li>\u57fa\u672c\u30af\u30e9\u30b9\u306e\u5b9a\u7fa9 \u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066<strong>\u57fa\u672c\u30af\u30e9\u30b9<\/strong>\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002 pythonCopy code<code>declarative_base<\/code><code>from sqlalchemy.ext.declarative import declarative_base Base = declarative_base()<\/code><\/li>\n\n\n\n<li>\u57fa\u672c\u30af\u30e9\u30b9\u304b\u3089\u7d99\u627f\u3059\u308b 1 \u3064\u4ee5\u4e0a\u306e Python \u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3059\u308b<strong>\u30c7\u30fc\u30bf \u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059<\/strong>\u3002 \u3053\u308c\u3089\u306e\u30af\u30e9\u30b9\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c6\u30fc\u30d6\u30eb\u306b\u30de\u30c3\u30d7\u3055\u308c\u307e\u3059\u3002 pythonCopy code<code>class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String)<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u521d\u671f\u5316<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30bb\u30c3\u30b7\u30e7\u30f3\u30af\u30e9\u30b9pythonCopy\u30b3\u30fc\u30c9\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b<\/strong><code>from sqlalchemy.orm import Session<\/code><\/li>\n\n\n\n<li>\u30bb\u30c3\u30b7\u30e7\u30f3 \u30aa\u30d6\u30b8\u30a7\u30af\u30c8<strong>\u306e\u4f5c\u6210<\/strong>&nbsp;\u3053\u306e\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u3001\u524d\u306b\u4f5c\u6210\u3057\u305f\u30a8\u30f3\u30b8\u30f3 \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3068\u3057\u3066\u6301\u3064\u65b0\u3057\u3044 Session \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002 pythonCopy code<code>Session<\/code><code>session = Session(bind=engine)<\/code><\/li>\n<\/ol>\n\n\n\n<p>\u3053\u308c\u3067\u3001SQLAlchemy \u306e\u57fa\u672c\u7684\u306a\u521d\u671f\u5316\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u306f\u5b8c\u4e86\u3067\u3059\u3002 \u3053\u308c\u3067\u3001\u3053\u306e\u30bb\u30c3\u30b7\u30e7\u30f3 \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092 CRUD (\u4f5c\u6210\u3001\u8aad\u307f\u53d6\u308a\u3001\u66f4\u65b0\u3001\u524a\u9664) \u64cd\u4f5c\u306b\u4f7f\u7528\u3057\u305f\u308a\u3001\u30a8\u30f3\u30b8\u30f3 \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f7f\u7528\u3057\u3066\u751f\u306e SQL \u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3057\u305f\u308a\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u308c\u3089\u306f\u6700\u3082\u57fa\u672c\u7684\u306a\u521d\u671f\u5316\u8a2d\u5b9a\u3067\u3059\u3002 \u5fc5\u8981\u306b\u5fdc\u3058\u3066\u3001\u63a5\u7d9a\u30d7\u30fc\u30eb\u8a2d\u5b9a\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5206\u96e2\u30ec\u30d9\u30eb\u306a\u3069\u3001\u3088\u308a\u9ad8\u5ea6\u306a\u69cb\u6210\u3092\u884c\u3046\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002 \u305f\u3060\u3057\u3001\u3053\u308c\u3089\u306e\u57fa\u672c\u8a2d\u5b9a\u306f\u901a\u5e38\u3001\u307b\u3068\u3093\u3069\u306e\u4e00\u822c\u7684\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u306b\u306f\u5341\u5206\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>ORM(\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u30de\u30c3\u30d4\u30f3\u30b0)\u30c1\u30e3\u30c3\u30c8GPT\u306e\u57fa\u672c\u6982\u5ff5\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ORM (\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 \u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb \u30de\u30c3\u30d4\u30f3\u30b0): \u57fa\u672c\u6982\u5ff5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">ORM\u3068\u306f\u4f55\u3067\u3059\u304b?<\/h4>\n\n\n\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u30de\u30c3\u30d4\u30f3\u30b0(ORM)\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3068\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u9593\u306b\u5b9f\u884c\u53ef\u80fd\u306a\u4eee\u60f3\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u624b\u6cd5\u3067\u3059\u3002 SQLAlchemy for Python \u3067\u306f\u3001ORM \u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30e2\u30c7\u30eb\u3068\u547c\u3070\u308c\u308b Python \u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c6\u30fc\u30d6\u30eb\u3092\u8868\u73fe\u304a\u3088\u3073\u64cd\u4f5c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u8981\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30e2\u30c7\u30eb<\/strong>:ORM\u3067\u306f\u3001\u30e2\u30c7\u30eb\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5\u3092\u5b9a\u7fa9\u3059\u308bPython\u30af\u30e9\u30b9\u3067\u3059\u3002 \u5404\u30e2\u30c7\u30eb\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5185\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u5fdc\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30bb\u30c3\u30b7\u30e7\u30f3<\/strong>: \u30bb\u30c3\u30b7\u30e7\u30f3\u306f\u3001\u6c38\u7d9a\u7684\u306a\u64cd\u4f5c\u3092\u51e6\u7406\u3059\u308b\u4f5c\u696d\u5358\u4f4d\u3067\u3059\u3002 \u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3057\u3066\u3001\u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u8ffd\u52a0\u3001\u5909\u66f4\u3001\u304a\u3088\u3073\u524a\u9664\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30af\u30a8\u30ea<\/strong>: \u30af\u30a8\u30ea \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002 SQLAlchemy \u3067\u306f\u3001\u8c4a\u5bcc\u306a API \u3092\u4f7f\u7528\u3057\u3066\u8907\u96d1\u306a SQL \u30af\u30a8\u30ea\u3092\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u305d\u308c\u306f\u3069\u306e\u3088\u3046\u306b\u6a5f\u80fd\u3057\u307e\u3059\u304b?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30e2\u30c7\u30eb\u306e<strong>\u5ba3\u8a00<\/strong>: \u307e\u305a\u3001\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 \u3053\u308c\u306f\u901a\u5e38\u3001 \u304b\u3089\u7d99\u627f\u3055\u308c\u305f Python \u30af\u30e9\u30b9\u3067\u3059\u3002 pythonCopy code<code>Base<\/code><code>from sqlalchemy import Column, Integer, String from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String)<\/code><\/li>\n\n\n\n<li><strong>\u30c6\u30fc\u30d6\u30eb\u306e\u4f5c\u6210<\/strong>: \u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3057\u305f\u3089\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>create_all<\/code><code>Base.metadata.create_all(engine)<\/code><\/li>\n\n\n\n<li><strong>\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u4f5c\u6210:<\/strong>&nbsp;\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u306e\u5bfe\u8a71\u3092\u958b\u59cb\u3059\u308b\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002 pythonCopy code<code>from sqlalchemy.orm import Session session = Session(bind=engine)<\/code><\/li>\n\n\n\n<li><strong>\u6c38\u7d9a\u5316\u3055\u308c\u305f\u64cd\u4f5c<\/strong>: \u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3057\u305f CRUD \u64cd\u4f5c\u3002\n<ul class=\"wp-block-list\">\n<li><strong>\u30ec\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3059\u308b<\/strong>&nbsp;python \u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b<code>new_user = User(name='John') session.add(new_user) session.commit()<\/code><\/li>\n\n\n\n<li><strong>\u30af\u30a8\u30ea \u30ec\u30b3\u30fc\u30c9<\/strong>&nbsp;pythonCopy \u30b3\u30fc\u30c9<code>users = session.query(User).filter_by(name='John').all()<\/code><\/li>\n\n\n\n<li><strong>\u30ec\u30b3\u30fc\u30c9\u306e<\/strong>&nbsp;python \u30b3\u30fc\u30c9\u3092\u66f4\u65b0\u3059\u308b\u30b3\u30d4\u30fc<code>user = session.query(User).filter_by(name='John').first() user.name = 'Johnny' session.commit()<\/code><\/li>\n\n\n\n<li><strong>\u30ec\u30b3\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b python\u30b3\u30fc\u30c9<\/strong>\u3092\u30b3\u30d4\u30fc\u3059\u308b<code>user = session.query(User).filter_by(name='Johnny').first() session.delete(user) session.commit()<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u62bd\u8c61\u5316\u30ec\u30a4\u30e4\u30fc:ORM\u306f\u3001SQL\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3067\u306f\u306a\u304f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u306e\u65b9\u6cd5\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u64cd\u4f5c\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u9ad8\u30ec\u30d9\u30eb\u306e<strong>\u62bd\u8c61\u5316\u30ec\u30a4\u30e4\u30fc<\/strong>\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9<strong>\u306b\u4f9d\u5b58\u3057\u306a\u3044<\/strong>:ORM\u306e\u62bd\u8c61\u5316\u306b\u3088\u308a\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306f\u7570\u306a\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7ba1\u7406\u30b7\u30b9\u30c6\u30e0\u3092\u7c21\u5358\u306b\u5207\u308a\u66ff\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3:ORM\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306f\u901a\u5e38\u3001SQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u306a\u3069\u306e<strong>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3<\/strong>\u554f\u984c\u3092\u51e6\u7406\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u4fdd\u5b88\u6027\u3068\u53ef\u8aad\u6027<\/strong>: ORM \u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30b3\u30fc\u30c9\u306e\u4fdd\u5b88\u3068\u8aad\u307f\u53d6\u308a\u304c\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002<\/li>\n\n\n\n<li>DRY<strong>(\u7e70\u308a\u8fd4\u3055\u306a\u3044):<\/strong>ORM\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001DRY\u306e\u539f\u5247\u306b\u6cbf\u3063\u3066\u3001\u30c7\u30fc\u30bf\u30e2\u30c7\u30eb\u30921\u304b\u6240\u3067\u5b9a\u7fa9\u3057\u3001\u8907\u6570\u306e\u5834\u6240\u3067\u518d\u5229\u7528\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406:ORM\u306f\u3001\u8907\u96d1\u306a\u64cd\u4f5c\u306e\u4fe1\u983c\u6027\u3092\u9ad8\u3081\u308b\u5f37\u529b\u306a<strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3<\/strong>\u7ba1\u7406\u6a5f\u80fd\u3092\u63d0\u4f9b\u3059\u308b\u3053\u3068\u304c\u3088\u304f\u3042\u308a\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<p>ORM \u306e\u3053\u308c\u3089\u306e\u57fa\u672c\u7684\u306a\u6982\u5ff5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u306b SQLAlchemy \u307e\u305f\u306f\u305d\u306e\u4ed6\u306e ORM \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u3088\u308a\u52b9\u679c\u7684\u306b\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u30b3\u30a2SQL\u30d3\u30eb\u30c9\u30b7\u30b9\u30c6\u30e0ChatGPT\u306e\u57fa\u790e\u3068\u306a\u308b\u6982\u5ff5\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30b3\u30a2 SQL \u30d3\u30eb\u30c9 \u30b7\u30b9\u30c6\u30e0: \u57fa\u672c\u6982\u5ff5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u30b3\u30a2SQL\u30d3\u30eb\u30c9\u30b7\u30b9\u30c6\u30e0\u3068\u306f\u4f55\u3067\u3059\u304b?<\/h4>\n\n\n\n<p>SQLAlchemy \u3067\u306f\u3001\u30b3\u30a2 SQL \u30d3\u30eb\u30c9 \u30b7\u30b9\u30c6\u30e0\u306f\u3001SQL \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u30d3\u30eb\u30c9\u304a\u3088\u3073\u5b9f\u884c\u3059\u308b\u305f\u3081\u306e\u4e00\u9023\u306e Python API \u3092\u63d0\u4f9b\u3059\u308b\u3001\u57fa\u306b\u306a\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u62bd\u8c61\u5316\u30ec\u30a4\u30e4\u30fc\u3067\u3059\u3002 ORM\u30ec\u30a4\u30e4\u30fc\u3068\u306f\u7570\u306a\u308a\u3001\u30b3\u30a2SQL\u30d3\u30eb\u30c9\u30b7\u30b9\u30c6\u30e0\u306f\u5143\u306eSQL\u306b\u8fd1\u304f\u3001\u67d4\u8edf\u6027\u3068\u5236\u5fa1\u6027\u304c\u5411\u4e0a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u8981\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30a8\u30f3\u30b8\u30f3:&nbsp;<strong>\u30a8\u30f3\u30b8\u30f3<\/strong>\u306f SQLAlchemy Core \u306e\u30b3\u30a2 \u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u3042\u308a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u306e\u63a5\u7d9a\u3092\u7dad\u6301\u3059\u308b\u5f79\u5272\u3092\u62c5\u3044\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30c0\u30a4\u30a2\u30ec\u30af\u30c8: \u30c0\u30a4\u30a2\u30ec\u30af\u30c8\u306f\u3001MySQL\u3001PostgreSQL \u306a\u3069\u306e\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30b7\u30b9\u30c6\u30e0\u306e SQL&nbsp;<strong>\u30c0\u30a4\u30a2\u30ec\u30af\u30c8<\/strong>\u3068 DBAPI \u3092\u51e6\u7406\u3059\u308b\u30a8\u30f3\u30b8\u30f3\u306e\u30b5\u30d6\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u3059\u3002<\/li>\n\n\n\n<li><strong>\u5f0f<\/strong>\u8a00\u8a9e:\u3053\u308c\u306f\u3001SQL\u30af\u30a8\u30ea\u3092\u69cb\u7bc9\u3059\u308b\u305f\u3081\u306ePython DSL(\u30c9\u30e1\u30a4\u30f3\u56fa\u6709\u8a00\u8a9e)\u3067\u3059\u3002<\/li>\n\n\n\n<li>MetaData:&nbsp;<strong>MetaData<\/strong>&nbsp;\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30b9\u30ad\u30fc\u30de\u306b\u95a2\u9023\u3059\u308b\u3059\u3079\u3066\u306e\u60c5\u5831 (\u30c6\u30fc\u30d6\u30eb\u3001\u5217\u3001\u5236\u7d04\u306a\u3069) \u3092\u683c\u7d0d\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30c6\u30fc\u30d6\u30eb: SQLAlchemy \u30b3\u30a2\u3067\u306f\u3001\u30c6\u30fc\u30d6\u30eb \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5185\u306e<strong>\u30c6\u30fc\u30d6\u30eb<\/strong>\u3092\u8868\u3059\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u5217\u3068\u30c7\u30fc\u30bf\u578b: Column \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u30c6\u30fc\u30d6\u30eb\u5185\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8868\u3057\u3001<strong>\u30c7\u30fc\u30bf\u578b<\/strong>\u306f\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u30c7\u30fc\u30bf\u578b\u3092\u8868\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u305d\u308c\u306f\u3069\u306e\u3088\u3046\u306b\u6a5f\u80fd\u3057\u307e\u3059\u304b?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30a8\u30f3\u30b8\u30f3<\/strong>python \u30b3\u30fc\u30c9\u3092\u4f5c\u6210\u3059\u308b<code>from sqlalchemy import create_engine engine = create_engine('dialect:\/\/username:password@host:port\/database')<\/code><\/li>\n\n\n\n<li><strong>\u30e1\u30bf\u30c7\u30fc\u30bf\u3068\u30c6\u30fc\u30d6\u30eb<\/strong>python \u3092\u5b9a\u7fa9\u3059\u308b\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b<code>from sqlalchemy import MetaData, Table, Column, Integer, String metadata = MetaData() users = Table('users', metadata, Column('id', Integer, primary_key=True), Column('name', String) )<\/code><\/li>\n\n\n\n<li><strong>SQL \u30af\u30a8\u30ea\u306e\u69cb\u7bc9\u3068\u5b9f\u884c<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>\u9078\u629e (\u9078\u629e)<\/strong>&nbsp;python\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b<code>from sqlalchemy import select s = select([users]) result = engine.execute(s)<\/code><\/li>\n\n\n\n<li>\u30d1\u30a4\u30bd\u30f3<strong>\u306e\u633f\u5165\u30b3\u30fc\u30c9<\/strong>\u3092\u30b3\u30d4\u30fc\u3059\u308b<code>from sqlalchemy import insert i = insert(users).values(name='John') engine.execute(i)<\/code><\/li>\n\n\n\n<li><strong>\u66f4\u65b0\u30d1\u30a4<\/strong>\u30bd\u30f3\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b<code>from sqlalchemy import update u = update(users).where(users.c.name == 'John').values(name='Johnny') engine.execute(u)<\/code><\/li>\n\n\n\n<li>\u30d1\u30a4\u30bd\u30f3\u30b3\u30d4\u30fc\u30b3\u30fc\u30c9<strong>\u3092\u524a\u9664\u3059\u308b<\/strong><code>from sqlalchemy import delete d = delete(users).where(users.c.name == 'Johnny') engine.execute(d)<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u67d4\u8edf\u6027<\/strong>: Core SQL \u30d3\u30eb\u30c9 \u30b7\u30b9\u30c6\u30e0\u306f\u3001\u307b\u307c\u3059\u3079\u3066\u306e\u7a2e\u985e\u306e SQL \u30af\u30a8\u30ea\u3092\u69cb\u7bc9\u3059\u308b\u305f\u3081\u306e\u975e\u5e38\u306b\u67d4\u8edf\u306a\u65b9\u6cd5\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb<\/strong>:\u7279\u306b\u30af\u30a8\u30ea\u3092\u624b\u52d5\u3067\u6700\u9069\u5316\u3057\u305f\u308a\u3001\u8907\u96d1\u306aSQL\u30ed\u30b8\u30c3\u30af\u3092\u5b9f\u884c\u3057\u305f\u308a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306b\u3001\u3088\u308a\u7d30\u304b\u304f\u5236\u5fa1\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u4f9d\u5b58\u3057\u306a\u3044<\/strong>: ORM \u3068\u540c\u69d8\u306b\u3001Core SQL \u30d3\u30eb\u30c9 \u30b7\u30b9\u30c6\u30e0\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u4f9d\u5b58\u3057\u307e\u305b\u3093\u3002<\/li>\n\n\n\n<li><strong>\u7d44\u307f\u5408\u308f\u305b\u53ef\u80fd\u6027<\/strong>: \u3055\u307e\u3056\u307e\u306a SQL \u5f0f\u3068\u64cd\u4f5c\u3092\u7c21\u5358\u306b\u7d44\u307f\u5408\u308f\u305b\u3066\u3001\u8907\u96d1\u306a\u30af\u30a8\u30ea\u3092\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u518d\u5229\u7528\u6027<\/strong>:\u30af\u30a8\u30ea\u306fPython\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5f62\u5f0f\u3067\u69cb\u7bc9\u3055\u308c\u308b\u305f\u3081\u3001\u7c21\u5358\u306b\u518d\u5229\u7528\u307e\u305f\u306f\u62e1\u5f35\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3<\/strong>: ORM \u3068\u540c\u69d8\u306b\u3001Core SQL \u30d3\u30eb\u30c9 \u30b7\u30b9\u30c6\u30e0\u3082 SQL \u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u3092\u9632\u3050\u305f\u3081\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3 \u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<p>SQLAlchemy Core SQL \u30d3\u30eb\u30c9 \u30b7\u30b9\u30c6\u30e0\u306e\u3053\u308c\u3089\u306e\u57fa\u672c\u7684\u306a\u6982\u5ff5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u7279\u306b\u67d4\u8edf\u6027\u3068\u5236\u5fa1\u304c\u5fc5\u8981\u306a\u5834\u5408\u306b\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30af\u30a8\u30ea\u3092\u3088\u308a\u52b9\u679c\u7684\u306b\u69cb\u7bc9\u304a\u3088\u3073\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>DBAPI\u30a4\u30f3\u30bf\u30e9\u30af\u30b7\u30e7\u30f3\u30ec\u30a4\u30e4\u30fcChatGPT\u306e\u57fa\u672c\u6982\u5ff5\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">DBAPI \u76f8\u4e92\u4f5c\u7528\u5c64: \u57fa\u672c\u6982\u5ff5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">DBAPI \u30a4\u30f3\u30bf\u30e9\u30af\u30b7\u30e7\u30f3 \u30ec\u30a4\u30e4\u30fc\u3068\u306f<\/h4>\n\n\n\n<p>DBAPI (\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 API) \u76f8\u4e92\u4f5c\u7528\u30ec\u30a4\u30e4\u30fc\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u306e\u76f4\u63a5\u901a\u4fe1\u3092\u62c5\u5f53\u3059\u308b SQLAlchemy \u306e\u57fa\u306b\u306a\u308b\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u3059\u3002 \u3053\u306e\u30ec\u30a4\u30e4\u30fc\u306f\u3001SQL \u30af\u30a8\u30ea\u306e\u5b9f\u884c\u3068\u7d50\u679c\u30bb\u30c3\u30c8\u306e\u8fd4\u9001\u3001\u304a\u3088\u3073\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u958b\u59cb\u3068\u7d42\u4e86\u3092\u51e6\u7406\u3057\u307e\u3059\u3002 \u3053\u308c\u306f\u3001\u3055\u307e\u3056\u307e\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u5bfe\u8a71\u3059\u308b\u305f\u3081\u306ePython DBAPI 2.0\u4ed5\u69d8\u306e\u5b9f\u88c5\u3067\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u8981\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u63a5\u7d9a\u30d7\u30fc\u30eb<\/strong>: \u63a5\u7d9a\u30d7\u30fc\u30eb\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u306e\u63a5\u7d9a\u3092\u7ba1\u7406\u3057\u3066\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u30ea\u30bd\u30fc\u30b9\u4f7f\u7528\u7387\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30ab\u30fc\u30bd\u30eb<\/strong>: \u30ab\u30fc\u30bd\u30eb \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001SQL \u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3057\u3066\u7d50\u679c\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3<\/strong>: \u3053\u306e\u90e8\u5206\u306f\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u958b\u59cb\u3001\u30b3\u30df\u30c3\u30c8\u3001\u304a\u3088\u3073\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u62c5\u5f53\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30a8\u30e9\u30fc\u51e6\u7406<\/strong>: DBAPI \u30ec\u30a4\u30e4\u30fc\u306f\u3001SQL \u30a8\u30e9\u30fc\u3084\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a\u306e\u554f\u984c\u306a\u3069\u306e\u30a8\u30e9\u30fc\u51e6\u7406\u3082\u62c5\u5f53\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u305d\u308c\u306f\u3069\u306e\u3088\u3046\u306b\u6a5f\u80fd\u3057\u307e\u3059\u304b?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>DBAPI \u5bfe\u8a71\u30ec\u30a4\u30e4\u30fc\u3067\u63a5\u7d9a\u3092\u53d6\u5f97\u3059\u308b\u306b\u306f\u3001\u901a\u5e38\u3001\u6700\u521d\u306e\u624b\u9806\u3068\u3057\u3066\u3001\u63a5\u7d9a\u30d7\u30fc\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a<strong>\u3092\u53d6\u5f97\u3057\u307e\u3059<\/strong>\u3002 pythonCopy code<code>connection = engine.connect()<\/code><\/li>\n\n\n\n<li>SQL \u30af\u30a8\u30ea\u306e\u5b9f\u884c \u53d6\u5f97\u3057\u305f\u63a5\u7d9a\u3092\u4f7f\u7528\u3057\u3066&nbsp;<strong>SQL \u30af\u30a8\u30ea<\/strong>\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002 pythonCopy \u30b3\u30fc\u30c9\u3092\u4f7f\u7528\u3059\u308b\u304b\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u5316\u3055\u308c\u305f\u30af\u30a8\u30ea\u3092\u4f7f\u7528\u3057\u3066\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3092\u5f37\u5316\u3057\u307e\u3059: pythonCopy code<code>result = connection.execute(\"SELECT * FROM users WHERE name = 'John'\")&nbsp;<\/code><code>result = connection.execute(\"SELECT * FROM users WHERE name = :name\", {'name': 'John'})<\/code><\/li>\n\n\n\n<li><strong>\u7d50\u679c\u306e\u51e6\u7406\u7d50\u679c<\/strong>\u30bb\u30c3\u30c8\u3092\u53cd\u5fa9\u51e6\u7406\u3057\u3066\u3001\u30af\u30a8\u30ea\u306b\u3088\u3063\u3066\u8fd4\u3055\u308c\u308b\u3059\u3079\u3066\u306e\u884c\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>for row in result: print(row)<\/code><\/li>\n\n\n\n<li><strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406\u306f<\/strong>&nbsp;DBAPI \u5c64\u306b\u3042\u308a\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u624b\u52d5\u3067\u7ba1\u7406\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>trans = connection.begin() try: connection.execute(\"INSERT INTO users (name) VALUES ('John')\") trans.commit() except: trans.rollback()<\/code><\/li>\n\n\n\n<li>\u63a5\u7d9a<strong>\u3092\u9589\u3058\u308b<\/strong>&nbsp;\u7d42\u4e86\u3057\u305f\u3089\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a\u3092\u9589\u3058\u307e\u3059\u3002 pythonCopy code<code>connection.close()<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9<\/strong>: DBAPI \u306f\u4f4e\u30ec\u30d9\u30eb\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u3067\u3042\u308b\u305f\u3081\u3001\u901a\u5e38\u306f\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u67d4\u8edf\u6027<\/strong>:\u3069\u3093\u306a\u306b\u8907\u96d1\u3067\u3082\u3001\u3042\u3089\u3086\u308bSQL\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u5236\u5fa1<\/strong>: DBAPI \u5c64\u306f\u3001\u7279\u306b\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406\u3068\u30a8\u30e9\u30fc\u51e6\u7406\u306b\u304a\u3044\u3066\u3001SQLAlchemy \u306e\u4ed6\u306e\u90e8\u5206\u3088\u308a\u3082\u8a73\u7d30\u306a\u5236\u5fa1\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u76f4\u63a5<\/strong>\u30a2\u30af\u30bb\u30b9: \u7279\u5225\u306a\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u305f\u308a\u3001\u30af\u30a8\u30ea\u3092\u6700\u9069\u5316\u3057\u305f\u308a\u3059\u308b\u305f\u3081\u306b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u76f4\u63a5\u30a2\u30af\u30bb\u30b9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u3001DBAPI \u304c\u9069\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u9593<\/strong>: DBAPI 2.0 \u4ed5\u69d8\u306e\u305f\u3081\u3001\u3053\u306e\u5c64\u3082\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u4f9d\u5b58\u3057\u307e\u305b\u3093\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b46\u8a18<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3<\/strong>:DBAPI\u30ec\u30a4\u30e4\u30fc\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001SQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u653b\u6483\u306e\u9632\u6b62\u306b\u3082\u3063\u3068\u6ce8\u610f\u3092\u6255\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u8907\u96d1\u3055<\/strong>: \u3053\u306e\u30ec\u30a4\u30e4\u30fc\u306f\u3001\u591a\u304f\u306e\u5834\u5408\u3001ORM \u3084 Core SQL \u30d3\u30eb\u30c9 \u30b7\u30b9\u30c6\u30e0\u3088\u308a\u3082\u4f7f\u7528\u3068\u4fdd\u5b88\u304c\u56f0\u96e3\u3067\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>SQLAlchemy \u306e DBAPI \u5bfe\u8a71\u30ec\u30a4\u30e4\u30fc\u306e\u3053\u308c\u3089\u306e\u57fa\u672c\u7684\u306a\u6982\u5ff5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u7279\u306b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u76f4\u63a5\u5bfe\u8a71\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u3084\u8907\u96d1\u306a\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u7ba1\u7406\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306b\u3001\u4f4e\u30ec\u30d9\u30eb\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30e2\u30c7\u30ebChatGPT\u3092\u4f5c\u6210\u30fb\u7ba1\u7406\u3059\u308b\u305f\u3081\u306e\u5b9a\u7fa9\u30e2\u30c7\u30eb\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u6559\u3048\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30e2\u30c7\u30eb\u306e\u4f5c\u6210\u3068\u7ba1\u7406: \u30e2\u30c7\u30eb\u306e\u5b9a\u7fa9<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30e2\u30c7\u30eb\u3068\u306f<\/h4>\n\n\n\n<p>SQLAlchemy\u306eORM(\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u30de\u30c3\u30d4\u30f3\u30b0)\u3067\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30e2\u30c7\u30eb\u306f\u901a\u5e38\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u5fdc\u3059\u308b\u69cb\u9020\u3068\u52d5\u4f5c\u3092\u5b9a\u7fa9\u3059\u308bPython\u30af\u30e9\u30b9\u3067\u3059\u3002 \u3053\u306e\u30af\u30e9\u30b9\u306f\u901a\u5e38\u3001SQLAlchemy \u306e\u30af\u30e9\u30b9\u304b\u3089\u7d99\u627f\u3057\u3001\u30af\u30e9\u30b9 \u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u4f7f\u7528\u3057\u3066\u30c6\u30fc\u30d6\u30eb\u5217\u3084\u305d\u306e\u4ed6\u306e\u5236\u7d04\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<code>Base<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u8981\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u57fa\u672c\u30af\u30e9\u30b9<\/strong>: \u3059\u3079\u3066\u306e\u30e2\u30c7\u30eb\u306f\u3001\u901a\u5e38\u306f\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u3063\u3066\u4f5c\u6210\u3055\u308c\u305f\u57fa\u672c\u30af\u30e9\u30b9\u304b\u3089\u7d99\u627f\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 pythonCopy code<code>declarative_base()<\/code><code>from sqlalchemy.ext.declarative import declarative_base Base = declarative_base()<\/code><\/li>\n\n\n\n<li><strong>\u30c6\u30fc\u30d6\u30eb\u540d<\/strong>: \u5404\u30e2\u30c7\u30eb \u30af\u30e9\u30b9\u306b\u306f\u3001\u5bfe\u5fdc\u3059\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c6\u30fc\u30d6\u30eb\u540d\u3092\u6307\u5b9a\u3059\u308b\u30d7\u30ed\u30d1\u30c6\u30a3\u304c\u5fc5\u8981\u3067\u3059\u3002<code>__tablename__<\/code><\/li>\n\n\n\n<li>\u5217:&nbsp;<strong>Columns<\/strong>&nbsp;\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u5217\u3092\u5b9a\u7fa9\u3057\u3001\u30c7\u30fc\u30bf\u578b\u3084\u305d\u306e\u4ed6\u306e\u5236\u7d04\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<code>Column<\/code><\/li>\n\n\n\n<li><strong>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7<\/strong>: \u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u30011 \u5bfe\u591a\u3001\u591a\u5bfe\u591a\u306a\u3069\u306e\u30e2\u30c7\u30eb\u9593\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<code>relationship<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u30e2\u30c7\u30eb\u306f\u3069\u306e\u3088\u3046\u306b\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u304b?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5fc5\u8981\u306a\u30e2\u30b8\u30e5\u30fc\u30eb<\/strong>\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b python\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b<code>from sqlalchemy import Column, Integer, String, ForeignKey from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship<\/code><\/li>\n\n\n\n<li><strong>\u57fa\u672c\u30af\u30e9\u30b9\u306e<\/strong>&nbsp;pythonCopy \u30b3\u30fc\u30c9\u3092\u4f5c\u6210\u3059\u308b<code>Base = declarative_base()<\/code><\/li>\n\n\n\n<li><strong>\u30e2\u30c7\u30eb\u30af\u30e9\u30b9<\/strong>pythonCopy\u30b3\u30fc\u30c9\u3092\u5b9a\u7fa9\u3059\u308b<code>class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String) email = Column(String, unique=True) # One-to-many relationship orders = relationship(\"Order\", back_populates=\"user\")<\/code><\/li>\n\n\n\n<li><strong>\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3 \u30e2\u30c7\u30eb\u306e\u5b9a\u7fa9<\/strong>\u4ed6\u306e\u30c6\u30fc\u30d6\u30eb\u3068\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u6301\u3064\u30e2\u30c7\u30eb\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u540c\u3058 Python \u30af\u30e9\u30b9\u307e\u305f\u306f\u7570\u306a\u308b Python \u30af\u30e9\u30b9\u3067\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>class Order(Base): __tablename__ = 'orders' id = Column(Integer, primary_key=True) total = Column(Integer) user_id = Column(Integer, ForeignKey('users.id')) # Back-populate user = relationship(\"User\", back_populates=\"orders\")<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u5236\u7d04\u3068\u305d\u306e\u4ed6\u306e\u30aa\u30d7\u30b7\u30e7\u30f3<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e3b\u30ad\u30fc<\/strong>: \u4e3b\u30ad\u30fc\u3092 \u3067\u30de\u30fc\u30af\u3057\u307e\u3059\u3002<code>primary_key=True<\/code><\/li>\n\n\n\n<li><strong>\u4e00\u610f<\/strong>: \u5217\u306e\u4e00\u610f\u6027\u3092\u78ba\u4fdd\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<code>unique=True<\/code><\/li>\n\n\n\n<li><strong>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9<\/strong>: \u30af\u30a8\u30ea\u3092\u9ad8\u901f\u5316\u3059\u308b\u305f\u3081\u306b\u5217\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<code>index=True<\/code><\/li>\n\n\n\n<li><strong>\u65e2\u5b9a\u5024<\/strong>: \u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3092\u4f7f\u7528\u3057\u3066\u3001\u5217\u306e\u65e2\u5b9a\u5024\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<code>default<\/code><\/li>\n\n\n\n<li><strong>Null \u8a31\u5bb9<\/strong>: \u5217\u306b NULL \u5024\u3092\u542b\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<code>nullable=False<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b<\/h4>\n\n\n\n<p>\u3059\u3079\u3066\u306e\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3057\u305f\u3089\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002<code>create_all<\/code><\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>Base.metadata.create_all(engine)<\/code><\/p>\n\n\n\n<p>\u3053\u308c\u306b\u3088\u308a\u3001\u5b9a\u7fa9\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u30e2\u30c7\u30eb\u306b\u57fa\u3065\u3044\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30c6\u30fc\u30d6\u30eb\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u62bd\u8c61\u5316<\/strong>: \u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u9ad8\u3044\u62bd\u8c61\u5316\u30ec\u30d9\u30eb\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u64cd\u4f5c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u4fdd\u5b88\u6027<\/strong>: \u30e2\u30c7\u30eb\u3092\u4f7f\u7528\u3057\u3066\u3001\u30b3\u30fc\u30c9\u306e\u7406\u89e3\u3068\u4fdd\u5b88\u3092\u5bb9\u6613\u306b\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf\u306e<\/strong>\u6574\u5408\u6027: \u30e2\u30c7\u30eb \u30ec\u30d9\u30eb\u3067\u5236\u7d04\u3068\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3092\u78ba\u4fdd\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>SQLAlchemy \u3067\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b\u65b9\u6cd5\u3092\u5b66\u7fd2\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30e2\u30c7\u30eb\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u4f5c\u6210\u304a\u3088\u3073\u7ba1\u7406\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u304c\u7c21\u7d20\u5316\u3055\u308c\u3001\u30b3\u30fc\u30c9\u54c1\u8cea\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30e2\u30c7\u30ebChatGPT\u3092\u4f5c\u6210\u304a\u3088\u3073\u7ba1\u7406\u3059\u308b\u305f\u3081\u306e\u4f5c\u6210\u304a\u3088\u3073\u524a\u9664\u30c6\u30fc\u30d6\u30eb\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30e2\u30c7\u30eb\u306e\u4f5c\u6210\u3068\u7ba1\u7406: \u30c6\u30fc\u30d6\u30eb\u306e\u4f5c\u6210\u3068\u524a\u9664<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b<\/h4>\n\n\n\n<p>SQLAlchemy\u3067\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30e2\u30c7\u30eb(\u3064\u307e\u308aPython\u30af\u30e9\u30b9)\u3092\u5b9a\u7fa9\u3057\u305f\u3089\u3001\u6b21\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u901a\u5e38\u3001\u305d\u308c\u3089\u306e\u30e2\u30c7\u30eb\u306b\u57fa\u3065\u3044\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u3067\u3059\u3002 \u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u306f\u3001\u901a\u5e38\u3001\u6b21\u306e\u624b\u9806\u304c\u542b\u307e\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5fc5\u8981\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3001python\u3092\u30e2\u30c7\u30eb\u5316\u3059\u308b\u30b3\u30fc\u30c9<\/strong>\u3092\u30b3\u30d4\u30fc\u3059\u308b<code>from sqlalchemy import create_engine from your_model_file import YourModel, Base # \u5047\u8bbe\u4f60\u7684\u6a21\u578b\u5728 'your_model_file' \u6587\u4ef6\u4e2d<\/code><\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30a8\u30f3\u30b8\u30f3\u306e<\/strong>&nbsp;python \u30b3\u30fc\u30c9\u3092\u4f5c\u6210\u3059\u308b<code>engine = create_engine('dialect:\/\/username:password@host:port\/database')<\/code><\/li>\n\n\n\n<li><strong>create_all \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068<code>\u3001<\/code><\/strong>\u5b9a\u7fa9\u3055\u308c\u305f\u3059\u3079\u3066\u306e\u30e2\u30c7\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30c6\u30fc\u30d6\u30eb\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002 pythonCopy code \u3053\u306e\u30b3\u30de\u30f3\u30c9\u306f\u3001\u30e2\u30c7\u30eb\u306b\u5bfe\u5fdc\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u304c\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u65e2\u306b\u5b58\u5728\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u3001\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306f\u65b0\u3057\u3044\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<code>create_all<\/code><code>Base.metadata.create_all(engine)<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c6\u30fc\u30d6\u30eb\u3092\u524a\u9664\u3059\u308b<\/h4>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c6\u30fc\u30d6\u30eb\u3092\u524a\u9664\u3059\u308b\u3068\u3001\u30c6\u30fc\u30d6\u30eb\u3068\u305d\u306e\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u304c\u5b8c\u5168\u306b\u524a\u9664\u3055\u308c\u308b\u305f\u3081\u3001\u614e\u91cd\u306a\u624b\u9806\u3067\u3059\u3002 SQLAlchemy \u3067\u306f\u3001\u307e\u305f\u306f\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30c6\u30fc\u30d6\u30eb\u3092\u524a\u9664\u3067\u304d\u307e\u3059\u3002<code>drop_all<\/code><code>drop<\/code><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>drop_all\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068<code>\u3001<\/code><\/strong>&nbsp;\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u3066\u3044\u308b\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30fb\u30c6\u30fc\u30d6\u30eb\u304c\u524a\u9664\u3055\u308c\u307e\u3059\u3002 pythonCopy \u30b3\u30fc\u30c9\u306e<strong>\u8b66\u544a: \u3053\u308c\u306b\u3088\u308a\u30011 \u3064\u3060\u3051\u3067\u306a\u304f\u3059\u3079\u3066\u306e\u30c6\u30fc\u30d6\u30eb\u304c\u524a\u9664\u3055\u308c\u307e\u3059\u3002&nbsp;<\/strong><code>drop_all<\/code><code>Base<\/code><code>Base.metadata.drop_all(engine)<\/code><\/li>\n\n\n\n<li><strong><code>drop<\/code>&nbsp;\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b<\/strong>&nbsp;\u7279\u5b9a\u306e\u30c6\u30fc\u30d6\u30eb\u3092 1 \u3064\u3060\u3051\u524a\u9664\u3059\u308b\u5834\u5408\u306f\u3001Object \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002 pythonCopy code \u3053\u3053\u3067\u306f\u3001\u524a\u9664\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u306e\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3067\u3059\u3002<code>Table<\/code><code>drop<\/code><code>YourModel.__table__.drop(engine)&nbsp;<\/code><code>YourModel<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b46\u8a18<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30d0\u30c3\u30af\u30a2\u30c3\u30d7:\u30c6\u30fc\u30d6\u30eb\u3092\u524a\u9664\u3059\u308b\u524d\u306b\u3001\u3059\u3079\u3066\u306e\u91cd\u8981\u306a\u30c7\u30fc\u30bf\u3092<strong>\u30d0\u30c3\u30af\u30a2\u30c3\u30d7<\/strong>\u3057\u305f\u3053\u3068\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/li>\n\n\n\n<li><strong>\u4f9d\u5b58\u95a2\u4fc2<\/strong>: \u30c6\u30fc\u30d6\u30eb\u3092\u524a\u9664\u3059\u308b\u524d\u306b\u3001\u4ed6\u306e\u30c6\u30fc\u30d6\u30eb\u304c\u305d\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u4f9d\u5b58\u3057\u3066\u3044\u306a\u3044\u3053\u3068\u3001\u307e\u305f\u306f\u305d\u308c\u3089\u306e\u4f9d\u5b58\u30c6\u30fc\u30d6\u30eb\u3082\u524a\u9664\u3059\u308b\u6e96\u5099\u304c\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/li>\n\n\n\n<li><strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3<\/strong>: \u554f\u984c\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306b\u4ee5\u524d\u306e\u72b6\u614b\u306b\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3067\u304d\u308b\u3088\u3046\u306b\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3067\u524a\u9664\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/li>\n\n\n\n<li><strong>\u30a2\u30af\u30bb\u30b9\u8a31\u53ef<\/strong>: \u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u307e\u305f\u306f\u524a\u9664\u3059\u308b\u305f\u3081\u306e\u5341\u5206\u306a\u30a2\u30af\u30bb\u30b9\u8a31\u53ef\u304c\u3042\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>SQLAlchemy \u3067\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u304a\u3088\u3073\u524a\u9664\u3059\u308b\u65b9\u6cd5\u3092\u5b66\u7fd2\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30e2\u30c7\u30eb\u3092\u3088\u308a\u52b9\u679c\u7684\u306b\u7ba1\u7406\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002 \u3053\u308c\u3089\u306e\u64cd\u4f5c\u306f\u3001\u591a\u304f\u306e\u5834\u5408\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u958b\u767a\u30d7\u30ed\u30bb\u30b9\u306e\u57fa\u672c\u7684\u306a\u624b\u9806\u3067\u3059\u304c\u3001\u975e\u5e38\u306b\u91cd\u8981\u3067\u6a5f\u5bc6\u6027\u306e\u9ad8\u3044\u64cd\u4f5c\u3067\u3082\u3042\u308b\u305f\u3081\u3001\u6ce8\u610f\u3057\u3066\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u30af\u30a8\u30ea\u30b7\u30b9\u30c6\u30e0\u306e\u57fa\u672c\u7684\u306a\u30af\u30a8\u30eaChatGPT\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30af\u30a8\u30ea \u30b7\u30b9\u30c6\u30e0: \u57fa\u672c\u30af\u30a8\u30ea<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u672c\u30af\u30a8\u30ea\u3068\u306f<\/h4>\n\n\n\n<p>SQLAlchemy\u306eORM\u3067\u306f\u3001\u57fa\u306b\u306a\u308b\u30af\u30a8\u30ea\u306f\u4e3b\u306b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u30af\u30e9\u30b9\u3092\u4ecb\u3057\u3066\u884c\u308f\u308c\u307e\u3059\u3002 \u3053\u308c\u3089\u306e\u30af\u30a8\u30ea\u306f\u3001\u901a\u5e38\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3001\u30d5\u30a3\u30eb\u30bf\u30fc\u51e6\u7406\u3001\u304a\u3088\u3073\u4e26\u3079\u66ff\u3048\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<code>Session<\/code><code>Query<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u8981\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Session<\/strong>: \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u30af\u30a8\u30ea\u3092\u542b\u3080\u3059\u3079\u3066\u306e ORM \u64cd\u4f5c\u306e\u958b\u59cb\u70b9\u3067\u3059\u3002 pythonCopy code<code>Session<\/code><code>from sqlalchemy.orm import Session session = Session(bind=engine)<\/code><\/li>\n\n\n\n<li><strong>\u30af\u30a8\u30ea\u30aa\u30d6\u30b8\u30a7\u30af\u30c8: \u30af\u30a8\u30ea\u30aa\u30d6\u30b8\u30a7\u30af\u30c8:<\/strong>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30af\u30a8\u30ea\u306e\u69cb\u7bc9\u3068\u5b9f\u884c\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002 pythonCopy code<code>Query<\/code><code>query = session.query(YourModel)<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u306b\u306a\u308b\u30af\u30a8\u30ea\u64cd\u4f5c<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u3059\u3079\u3066\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u9078\u629e\u3059\u308b<\/strong>&nbsp;python\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b<code>all_records = session.query(YourModel).all()<\/code><\/li>\n\n\n\n<li><strong>\u30ec\u30b3\u30fc\u30c9\u3092\u9078\u629e\u3059\u308b python\u30b3\u30fc\u30c9<\/strong>\u3092\u30b3\u30d4\u30fc\u3059\u308b or python\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b<code>first_record = session.query(YourModel).first()&nbsp;<\/code><code>one_record = session.query(YourModel).filter_by(id=1).one()<\/code><\/li>\n\n\n\n<li>or \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066<strong>\u30ec\u30b3\u30fc\u30c9\u3092\u30d5\u30a3\u30eb\u30bf\u30fc\u51e6\u7406<\/strong>\u3057\u307e\u3059\u3002 python\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b\u304b\u3001python\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b\u304b<code>filter<\/code><code>filter_by<\/code><code>filtered_records = session.query(YourModel).filter(YourModel.name == 'John').all()&nbsp;<\/code><code>filtered_records = session.query(YourModel).filter_by(name='John').all()<\/code><\/li>\n\n\n\n<li>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066<strong>\u30ec\u30b3\u30fc\u30c9\u3092\u4e26\u3079\u66ff\u3048\u307e\u3059<\/strong>\u3002 pythonCopy code<code>order_by<\/code><code>sorted_records = session.query(YourModel).order_by(YourModel.name).all()<\/code><\/li>\n\n\n\n<li>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066<strong>\u30ab\u30a6\u30f3\u30c8\u3057\u307e\u3059<\/strong>\u3002 pythonCopy code<code>count<\/code><code>record_count = session.query(YourModel).count()<\/code><\/li>\n\n\n\n<li><strong>\u6539\u30da\u30fc\u30b8\u4f4d\u7f6e\u306e\u81ea\u52d5\u4fee\u6b63<\/strong>\u3067\u306f\u3001and \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002 pythonCopy code<code>limit<\/code><code>offset<\/code><code>paginated_records = session.query(YourModel).limit(10).offset(20).all()<\/code><\/li>\n\n\n\n<li>, \u306a\u3069\u306e<strong>\u96c6\u8a08\u95a2\u6570<\/strong>\u3002 pythonCopy code<code>sum<\/code><code>avg<\/code><code>from sqlalchemy import func total = session.query(func.sum(YourModel.field)).scalar()<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c1\u30a7\u30fc\u30f3\u901a\u8a71<\/h4>\n\n\n\n<p>\u8907\u6570\u306e\u30af\u30a8\u30ea \u30e1\u30bd\u30c3\u30c9\u3092\u9023\u7d50\u3057\u3066\u3001\u3088\u308a\u8907\u96d1\u306a\u30af\u30a8\u30ea\u3092\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>complex_query = session.query(YourModel).filter_by(name='John').order_by(YourModel.id).limit(10).all()<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u62bd\u8c61\u5316<\/strong>: SQLAlchemy \u306e\u30af\u30a8\u30ea \u30b7\u30b9\u30c6\u30e0\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001SQL \u30b3\u30fc\u30c9\u3092\u76f4\u63a5\u8a18\u8ff0\u3057\u306a\u304f\u3066\u3082\u3001\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027<\/strong>:Pythonic\u306eAPI\u3068\u30c1\u30a7\u30fc\u30f3\u547c\u3073\u51fa\u3057\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30b3\u30fc\u30c9\u306e\u8aad\u307f\u53d6\u308a\u3068\u4fdd\u5b88\u304c\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3<\/strong>: SQLAlchemy \u306e\u30af\u30a8\u30ea \u30b7\u30b9\u30c6\u30e0\u306f\u3001SQL \u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u653b\u6483\u306e\u9632\u6b62\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>SQLAlchemy \u306e\u57fa\u306b\u306a\u308b\u30af\u30a8\u30ea\u64cd\u4f5c\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001SQL \u30b3\u30fc\u30c9\u3092\u76f4\u63a5\u8a18\u8ff0\u3057\u306a\u304f\u3066\u3082\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u3088\u308a\u52b9\u7387\u7684\u306b\u60c5\u5831\u3092\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002 \u3053\u308c\u306b\u3088\u308a\u3001\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u304c\u5411\u4e0a\u3059\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3082\u5411\u4e0a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u30af\u30a8\u30ea\u30b7\u30b9\u30c6\u30e0ChatGPT\u306e\u8907\u96d1\u306a\u30af\u30a8\u30ea\u3068\u30b5\u30d6\u30af\u30a8\u30ea\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30af\u30a8\u30ea \u30b7\u30b9\u30c6\u30e0: \u8907\u96d1\u306a\u30af\u30a8\u30ea\u3068\u30b5\u30d6\u30af\u30a8\u30ea<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u8907\u96d1\u306a\u30af\u30a8\u30ea<\/h4>\n\n\n\n<p>SQLAlchemy \u3067\u306f\u3001\u57fa\u672c\u7684\u306a\u30af\u30a8\u30ea\u306b\u52a0\u3048\u3066\u3001\u7d50\u5408\u3001\u30b0\u30eb\u30fc\u30d7\u5316\u3001\u30b5\u30d6\u30af\u30a8\u30ea\u306a\u3069\u3001\u3088\u308a\u8907\u96d1\u306a\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7d50\u5408\u3067\u306f\u3001\u30c6\u30fc\u30d6\u30eb<strong>\u7d50\u5408<\/strong>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002 python\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b\u304b\u3001\u5916\u90e8\u7d50\u5408\u3092\u884c\u3046\u305f\u3081\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002 pythonCopy code<code>join<\/code><code>from sqlalchemy.orm import joinedload query = session.query(User).join(Order).filter(Order.amount &gt; 100)&nbsp;<\/code><code>outerjoin<\/code><code>query = session.query(User).outerjoin(Order).filter(Order.amount == None)<\/code><\/li>\n\n\n\n<li><strong>[\u30b0\u30eb\u30fc\u30d7\u5316]<\/strong>&nbsp;\u3067\u306f\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u307e\u3059\u3002 pythonCopy code<code>group_by<\/code><code>from sqlalchemy import func query = session.query(User.name, func.count(Order.id)).join(Order).group_by(User.name)<\/code><\/li>\n\n\n\n<li><strong>Having \u53e5\u306f<\/strong>\u3001HAVING \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066 HAVING \u53e5\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002 pythonCopy code<code>having<\/code><code>query = session.query(User.name, func.count(Order.id)).join(Order).group_by(User.name).having(func.count(Order.id) &gt; 2)<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u30b5\u30d6\u30af\u30a8\u30ea<\/h4>\n\n\n\n<p>\u30b5\u30d6\u30af\u30a8\u30ea\u306f\u3001\u4ed6\u306e\u30af\u30a8\u30ea\u5185\u306b\u30cd\u30b9\u30c8\u3055\u308c\u305f\u30af\u30a8\u30ea\u3067\u3059\u3002 SQLAlchemy \u3067\u306f\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002<code>subquery<\/code><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30b9\u30ab\u30e9\u30fc\u91cf\u5b50<strong>\u30af\u30a8\u30ea \u30b9\u30ab\u30e9\u30fc\u91cf\u5b50\u30af\u30a8\u30ea<\/strong>\u306f\u3001\u5358\u4e00\u306e\u5024\u3092\u8fd4\u3057\u307e\u3059\u3002 \u305f\u3068\u3048\u3070\u3001\u5404\u30e6\u30fc\u30b6\u30fc\u306e\u6700\u5f8c\u306e\u6ce8\u6587\u3092\u898b\u3064\u3051\u307e\u3059\u3002 pythonCopy code<code>from sqlalchemy import desc subq = session.query(func.max(Order.date)).filter(Order.user_id == User.id).scalar_subquery() query = session.query(User, subq.label(\"last_order_date\"))<\/code><\/li>\n\n\n\n<li>\u30a2\u30bd<strong>\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u30b5\u30d6\u30af\u30a8\u30ea\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u30b5\u30d6\u30af\u30a8\u30ea<\/strong>\u306f\u3001\u30e1\u30a4\u30f3\u30af\u30a8\u30ea\u3067\u4f7f\u7528\u3055\u308c\u3001\u901a\u5e38\u306f\u3001\u3001\u307e\u305f\u306f \u306a\u3069\u306e\u6f14\u7b97\u5b50\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002 pythonCopy code<code>exists<\/code><code>any<\/code><code>all<\/code><code>from sqlalchemy import exists subq = session.query(Order.id).filter(Order.user_id == User.id, Order.amount &gt; 100).exists() query = session.query(User).filter(subq)<\/code><\/li>\n\n\n\n<li>\u5217\u3068\u3057\u3066\u306e\u30b5\u30d6\u30af\u30a8\u30ea: \u30b5\u30d6\u30af\u30a8\u30ea\u3092 1 \u3064\u4ee5\u4e0a\u306e\u5217<strong>\u3068\u3057\u3066\u30e1\u30a4\u30f3 \u30af\u30a8\u30ea<\/strong>\u306b\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>subq = session.query(func.count(Order.id)).filter(Order.user_id == User.id).label(\"total_orders\") query = session.query(User, subq).group_by(User.id)<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c1\u30a7\u30fc\u30f3\u901a\u8a71<\/h4>\n\n\n\n<p>\u57fa\u306b\u306a\u308b\u30af\u30a8\u30ea\u3068\u540c\u69d8\u306b\u3001\u8907\u6570\u306e\u8907\u96d1\u306a\u30af\u30a8\u30ea \u30e1\u30bd\u30c3\u30c9\u3092\u9023\u7d50\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<p><code>query = session.query(User).join(Order).filter(Order.amount &gt; 100).group_by(User.name).having(func.count(Order.id) &gt; 2)<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u67d4\u8edf\u6027<\/strong>: \u8907\u96d1\u306a\u30af\u30a8\u30ea\u3068\u30b5\u30d6\u30af\u30a8\u30ea\u306f\u975e\u5e38\u306b\u9ad8\u3044\u67d4\u8edf\u6027\u3092\u63d0\u4f9b\u3057\u3001\u307b\u307c\u3059\u3079\u3066\u306e\u7a2e\u985e\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u6700\u9069\u5316<\/strong>: \u30b5\u30d6\u30af\u30a8\u30ea\u3068\u7d50\u5408\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u3092\u6700\u9069\u5316\u3057\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u8ee2\u9001\u3068\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u8ca0\u8377\u3092\u8efd\u6e1b\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30b3\u30f3\u30dd\u30fc\u30b6\u30d6\u30eb<\/strong>: \u57fa\u672c\u7684\u306a\u30af\u30a8\u30ea\u3068\u8907\u96d1\u306a\u30af\u30a8\u30ea\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u3001\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u53d6\u5f97\u30cb\u30fc\u30ba\u3092\u6e80\u305f\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>SQLAlchemy \u306e\u8907\u96d1\u306a\u30af\u30a8\u30ea\u3068\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u9ad8\u5ea6\u3067\u67d4\u8edf\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30af\u30a8\u30ea\u3092\u69cb\u7bc9\u3057\u3066\u3001\u30c7\u30fc\u30bf\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u53d6\u5f97\u304a\u3088\u3073\u5206\u6790\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002 \u3053\u308c\u3089\u306e\u9ad8\u5ea6\u306a\u6a5f\u80fd\u306b\u3088\u308a\u3001SQLAlchemy \u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5bfe\u8a71\u306e\u305f\u3081\u306e\u975e\u5e38\u306b\u5f37\u529b\u306a\u30c4\u30fc\u30eb\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u95a2\u4fc2\u3068\u7d50\u5408\u304cChatGPT\u3067\u95a2\u4fc2\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5\u3092\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3068\u7d50\u5408: \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u95a2\u4fc2\u3068\u306f\u4f55\u3067\u3059\u304b?<\/h4>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067\u306f\u3001\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u306f 2 \u3064\u4ee5\u4e0a\u306e\u30c6\u30fc\u30d6\u30eb\u9593\u306e\u8ad6\u7406\u63a5\u7d9a\u3067\u3059\u3002 SQLAlchemy \u306e ORM \u3067\u306f\u3001\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u30011 \u5bfe 1\u30011 \u5bfe\u591a\u3001\u591a\u5bfe\u591a\u306a\u3069\u306e\u30e2\u30c7\u30eb\u9593\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u5b9a\u7fa9\u3067\u304d\u307e\u3059\u3002<code>relationship<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u8981\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5916\u90e8\u30ad\u30fc<\/strong>: SQLAlchemy \u3067\u306f\u3001\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u5916\u90e8\u30ad\u30fc\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002 pythonCopy code<code>ForeignKey<\/code><code>from sqlalchemy import ForeignKey user_id = Column(Integer, ForeignKey('users.id'))<\/code><\/li>\n\n\n\n<li><strong>\u95a2\u4fc2<\/strong>: \u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3001\u30e2\u30c7\u30eb\u9593\u306e\u8ad6\u7406\u7684\u306a\u95a2\u4fc2\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002 pythonCopy code<code>relationship<\/code><code>from sqlalchemy.orm import relationship orders = relationship(\"Order\", back_populates=\"user\")<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u4f5c\u6210\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3088\u3044\u3067\u3059\u304b?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>1 \u5bfe 1 \u306e<strong>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u4e00\u5bfe\u4e00\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3067\u306f<\/strong>\u30012 \u3064\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u5404\u884c\u306b\u306f\u3001\u3082\u3046\u4e00\u65b9\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u3068\u306e\u95a2\u9023\u4ed8\u3051\u304c 1 \u3064\u3060\u3051\u3042\u308a\u307e\u3059\u3002 pythonCopy code<code>class Parent(Base): __tablename__ = 'parents' id = Column(Integer, primary_key=True) child = relationship(\"Child\", uselist=False, back_populates=\"parent\") class Child(Base): __tablename__ = 'children' id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey('parents.id')) parent = relationship(\"Parent\", back_populates=\"child\")<\/code><\/li>\n\n\n\n<li>\u4e00\u5bfe\u591a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7<strong>\u4e00\u5bfe\u591a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3067\u306f<\/strong>\u3001\u3042\u308b\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u3092\u5225\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u8907\u6570\u306e\u884c\u306b\u95a2\u9023\u4ed8\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>class Customer(Base): __tablename__ = 'customers' id = Column(Integer, primary_key=True) orders = relationship(\"Order\", back_populates=\"customer\") class Order(Base): __tablename__ = 'orders' id = Column(Integer, primary_key=True) customer_id = Column(Integer, ForeignKey('customers.id')) customer = relationship(\"Customer\", back_populates=\"orders\")<\/code><\/li>\n\n\n\n<li>\u591a\u5bfe\u591a<strong>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u591a\u5bfe\u591a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3067\u306f<\/strong>\u3001\u3042\u308b\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u3092\u5225\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u8907\u6570\u306e\u884c\u306b\u95a2\u9023\u4ed8\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u305d\u306e\u9006\u3082\u53ef\u80fd\u3067\u3059\u3002 pythonCopy code<code>association_table = Table('association', Base.metadata, Column('student_id', Integer, ForeignKey('students.id')), Column('course_id', Integer, ForeignKey('courses.id')) ) class Student(Base): __tablename__ = 'students' id = Column(Integer, primary_key=True) courses = relationship(\"Course\", secondary=association_table, back_populates=\"students\") class Course(Base): __tablename__ = 'courses' id = Column(Integer, primary_key=True) students = relationship(\"Student\", secondary=association_table, back_populates=\"courses\")<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u8a73\u7d30\u30aa\u30d7\u30b7\u30e7\u30f3<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9045\u5ef6\u8aad\u307f\u8fbc\u307f<\/strong>: \u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3092\u4f7f\u7528\u3057\u3066\u3001\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8aad\u307f\u8fbc\u3080\u30bf\u30a4\u30df\u30f3\u30b0\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002 \u305f\u3068\u3048\u3070\u3001\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u5143\u306e\u30af\u30a8\u30ea\u306b\u3059\u3050\u306b\u8aad\u307f\u8fbc\u307e\u308c\u307e\u3059\u3002<code>lazy<\/code><code>lazy='joined'<\/code><\/li>\n\n\n\n<li><strong>\u30ab\u30b9\u30b1\u30fc\u30c9<\/strong>: \u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3092\u4f7f\u7528\u3057\u3066\u3001\u524a\u9664\u306a\u3069\u306e\u30ab\u30b9\u30b1\u30fc\u30c9\u64cd\u4f5c\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<code>cascade<\/code><\/li>\n\n\n\n<li><strong>\u30d0\u30c3\u30af \u30c7\u30fc\u30bf:<\/strong>&nbsp;\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3092\u4f7f\u7528\u3057\u3066\u3001\u53cc\u65b9\u5411\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<code>back_populates<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u30c7\u30fc\u30bf\u306e<\/strong>\u6574\u5408\u6027: \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3068\u5916\u90e8\u30ad\u30fc\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c7\u30fc\u30bf\u6574\u5408\u6027\u3092\u78ba\u4fdd\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u3044\u3084\u3059\u3055<\/strong>: SQLAlchemy \u306e\u95a2\u6570\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001Python \u3067\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u5b9a\u7fa9\u3067\u304d\u307e\u3059\u3002<code>relationship<\/code><\/li>\n\n\n\n<li><strong>\u30af\u30a8\u30ea\u306e\u6700\u9069\u5316<\/strong>: \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u5b9a\u7fa9\u3057\u305f\u5f8c\u3001\u7d50\u5408\u30af\u30a8\u30ea\u3092\u3088\u308a\u7c21\u5358\u306b\u5b9f\u884c\u3057\u3066\u3001\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u3092\u6700\u9069\u5316\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>SQLAlchemy \u3067\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u73fe\u5b9f\u4e16\u754c\u306e\u8907\u96d1\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u3088\u308a\u52b9\u679c\u7684\u306b\u30b7\u30df\u30e5\u30ec\u30fc\u30c8\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u8a2d\u8a08\u306e\u5805\u7262\u6027\u3068\u67d4\u8edf\u6027\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002 \u3053\u308c\u306b\u3088\u308a\u3001\u30af\u30a8\u30ea\u3068\u30c7\u30fc\u30bf\u64cd\u4f5c\u304c\u5927\u5e45\u306b\u7c21\u7d20\u5316\u3055\u308c\u3001\u958b\u767a\u52b9\u7387\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u95a2\u4fc2\u3068\u7d50\u5408\u306e\u305f\u3081\u306bChatGPT\u3067\u7d50\u5408\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u65b9\u6cd5\u3092\u8a73\u7d30\u306b\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3068\u7d50\u5408: \u7d50\u5408\u64cd\u4f5c\u306e\u5b9f\u884c\u65b9\u6cd5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u7d50\u5408\u64cd\u4f5c\u3068\u306f<\/h4>\n\n\n\n<p>\u7d50\u5408\u64cd\u4f5c\u306f\u3001\u6761\u4ef6\u306b\u57fa\u3065\u3044\u3066 2 \u3064\u4ee5\u4e0a\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u7d50\u5408\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002 SQLAlchemy \u3067\u306f\u3001\u30e1\u30bd\u30c3\u30c9\u3084\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u305f\u308a\u3001\u30e1\u30bd\u30c3\u30c9\u3067\u7d50\u5408\u6761\u4ef6\u3092\u76f4\u63a5\u6307\u5b9a\u3057\u305f\u308a\u3059\u308b\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u65b9\u6cd5\u3067\u7d50\u5408\u64cd\u4f5c\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<code>join<\/code><code>outerjoin<\/code><code>filter<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u306a\u65b9\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>join<\/strong>: \u5185\u90e8\u7d50\u5408\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002 pythonCopy code<code>join<\/code><code>query = session.query(User).join(Order).filter(Order.amount &gt; 100)<\/code><\/li>\n\n\n\n<li><strong>\u5916\u90e8\u7d50\u5408<\/strong>: \u5de6\u5916\u90e8\u7d50\u5408\u65b9\u6cd5\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002 pythonCopy code<code>outerjoin<\/code><code>query = session.query(User).outerjoin(Order).filter(Order.amount == None)<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u7d50\u5408\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3088\u3044\u3067\u3059\u304b?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5358\u7d14\u306a\u5185\u90e8\u7d50\u5408<\/strong>pythonCopy\u30b3\u30fc\u30c9\u3053\u3053\u3067\u306f\u3001\u305d\u308c\u3089\u306b\u57fa\u3065\u3044\u3066\u5916\u90e8\u30ad\u30fc\u95a2\u4fc2\u3092\u6301\u30642\u3064\u306e\u7570\u306a\u308b\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3067\u3059\u3002<code>from sqlalchemy.orm import joinedload query = session.query(User).join(Order).filter(Order.amount &gt; 100)&nbsp;<\/code><code>User<\/code><code>Order<\/code><code>user_id<\/code><\/li>\n\n\n\n<li><strong><\/strong>\u7d50\u5408\u6761\u4ef6\u3092\u660e\u793a\u7684\u306b\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u6b21\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<code>query = session.query(User).join(Order, User.id == Order.user_id)<\/code><\/li>\n\n\n\n<li><strong>\u8907\u6570\u30c6\u30fc\u30d6\u30eb\u306e\u7d50\u5408\u4e00\u5ea6\u306b<\/strong>\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>query = session.query(User).join(Order).join(Address).filter(Order.amount &gt; 100, Address.city == 'New York')<\/code><\/li>\n\n\n\n<li><strong>\u5de6\u5916\u90e8\u30b8\u30e7\u30a4\u30f3\u5de6\u5916\u90e8\u30b8\u30e7\u30a4\u30f3\u3067\u306f<\/strong>\u3001\u53f3\u30c6\u30fc\u30d6\u30eb () \u306b\u4e00\u81f4\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u304c\u306a\u3044\u5834\u5408\u3067\u3082\u3001\u5de6\u30c6\u30fc\u30d6\u30eb () \u306e\u3059\u3079\u3066\u306e\u30ec\u30b3\u30fc\u30c9\u304c\u8fd4\u3055\u308c\u307e\u3059\u3002 pythonCopy code<code>Order<\/code><code>User<\/code><code>query = session.query(User).outerjoin(Order).filter(Order.amount == None)<\/code><\/li>\n\n\n\n<li>\u7279\u5b9a\u306e\u30ab\u30e9\u30e0\u306e<strong>\u7d50\u5408\u3068\u9078\u629e<\/strong>\u7d50\u679c\u30bb\u30c3\u30c8\u5185\u306e\u7279\u5b9a\u306e\u30ab\u30e9\u30e0\u306e\u307f\u306b\u95a2\u5fc3\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u305d\u308c\u3089\u306e\u30ab\u30e9\u30e0\u3092\u660e\u793a\u7684\u306b\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>query = session.query(User.name, Order.amount).join(Order).filter(Order.amount &gt; 100)<\/code><\/li>\n\n\n\n<li><strong>\u30a8\u30a4\u30ea\u30a2\u30b9\u3067\u7d50\u5408\u30af\u30a8\u30ea<code>\u3067<\/code><\/strong>\u540c\u3058\u30c6\u30fc\u30d6\u30eb\u3092\u8907\u6570\u56de\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u3001[\u30a8\u30a4\u30ea\u30a2\u30b9\u306e\u4f5c\u6210] \u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>aliased<\/code><code>from sqlalchemy.orm import aliased OrderAlias = aliased(Order) query = session.query(User, OrderAlias).join(OrderAlias, User.orders)<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u30c7\u30fc\u30bf\u7d71\u5408<\/strong>: \u7d50\u5408\u64cd\u4f5c\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u7d50\u5408\u3057\u3066\u3001\u3088\u308a\u8907\u96d1\u306a\u30af\u30a8\u30ea\u3068\u5206\u6790\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u6700\u9069\u5316<\/strong>: \u7d50\u5408\u3092\u8ce2\u304f\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u30c7\u30fc\u30bf\u306e\u91cf\u3092\u6e1b\u3089\u3057\u3001\u30af\u30a8\u30ea\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u67d4\u8edf\u6027<\/strong>: SQLAlchemy \u306b\u306f\u3001\u5185\u90e8\u7d50\u5408\u3001\u5916\u90e8\u7d50\u5408\u3001\u81ea\u5df1\u7d50\u5408\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u7d50\u5408\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u7528\u610f\u3055\u308c\u3066\u304a\u308a\u3001\u3055\u307e\u3056\u307e\u306a\u30af\u30a8\u30ea \u30cb\u30fc\u30ba\u3092\u6e80\u305f\u3059\u67d4\u8edf\u6027\u304c\u5f97\u3089\u308c\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>SQLAlchemy \u3067\u7d50\u5408\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u65b9\u6cd5\u3092\u5b66\u7fd2\u3059\u308b\u3053\u3068\u3067\u3001\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u3088\u308a\u52b9\u7387\u7684\u306b\u53d6\u5f97\u304a\u3088\u3073\u7d71\u5408\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002 \u3053\u308c\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30af\u30a8\u30ea\u306e\u975e\u5e38\u306b\u91cd\u8981\u306a\u5074\u9762\u3067\u3042\u308a\u3001\u30de\u30b9\u30bf\u30fc\u3059\u308b\u3068\u30c7\u30fc\u30bf\u64cd\u4f5c\u6a5f\u80fd\u304c\u5927\u5e45\u306b\u5411\u4e0a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>ChatGPT\u3067\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406\u304c\u3069\u306e\u3088\u3046\u306b\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u958b\u59cb\u304a\u3088\u3073\u7d42\u4e86\u3059\u308b\u304b\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406:\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u958b\u59cb\u304a\u3088\u3073\u7d42\u4e86\u3059\u308b\u65b9\u6cd5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068\u306f<\/h4>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7ba1\u7406\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u6574\u5408\u6027\u3092\u78ba\u4fdd\u3059\u308b\u305f\u3081\u306b\u6210\u529f\u307e\u305f\u306f\u5931\u6557\u3059\u308b 1 \u3064\u4ee5\u4e0a\u306e SQL \u64cd\u4f5c\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u3059\u3002 SQLAlchemy \u3067\u306f\u3001\u901a\u5e38\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4ecb\u3057\u3066\u884c\u308f\u308c\u307e\u3059\u3002<code>Session<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u8981\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30bb\u30c3\u30b7\u30e7\u30f3<\/strong>: SQLAlchemy \u306e ORM \u3067\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406\u3092\u542b\u3080\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5bfe\u8a71\u306e\u958b\u59cb\u70b9\u3067\u3059\u3002 pythonCopy code<code>Session<\/code><code>from sqlalchemy.orm import Session session = Session(bind=engine)<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u53d6\u5f15\u3092\u958b\u59cb\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3088\u3044\u3067\u3059\u304b?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u81ea\u52d5\u958b\u59cb<\/strong>&nbsp;\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c (\u30ec\u30b3\u30fc\u30c9\u306e\u8ffd\u52a0\u3001\u5909\u66f4\u3001\u524a\u9664\u306a\u3069) \u3092\u521d\u3081\u3066\u5b9f\u884c\u3059\u308b\u3068\u3001SQLAlchemy \u306b\u3088\u3063\u3066\u65b0\u3057\u3044\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u81ea\u52d5\u7684\u306b\u958b\u59cb\u3055\u308c\u307e\u3059\u3002 pythonCopy code \u3053\u306e\u4f8b\u3067\u306f\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3068\u3001\u65b0\u3057\u3044\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u81ea\u52d5\u7684\u306b\u958b\u59cb\u3055\u308c\u307e\u3059\u3002<code>Session<\/code><code>new_user = User(name='John', email='john@example.com') session.add(new_user)&nbsp;<\/code><code>session.add()<\/code><\/li>\n\n\n\n<li>\u624b\u52d5\u3067\u958b\u59cb\u3059\u308b\u304b\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u624b\u52d5\u3067<strong>\u958b\u59cb<\/strong>\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>begin<\/code><code>transaction = session.begin()<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u7d42\u4e86\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3088\u3044\u3067\u3059\u304b?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u30b3\u30df\u30c3\u30c8\u3067\u306f<\/strong>\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u30b3\u30df\u30c3\u30c8\u3057\u3001\u3059\u3079\u3066\u306e\u5909\u66f4\u3092\u4fdd\u5b58\u3057\u3066\u73fe\u5728\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u7d42\u4e86\u3057\u307e\u3059\u3002 pythonCopy \u30b3\u30fc\u30c9 \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u6b63\u5e38\u306b\u30b3\u30df\u30c3\u30c8\u3055\u308c\u308b\u3068\u3001\u305d\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3067\u884c\u308f\u308c\u305f\u3059\u3079\u3066\u306e\u5909\u66f4\u304c\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u6c38\u7d9a\u5316\u3055\u308c\u307e\u3059\u3002<code>commit<\/code><code>session.commit()<\/code><\/li>\n\n\n\n<li>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3<strong>\u3092\u30ed\u30fc\u30eb\u30d0\u30c3\u30af<\/strong>\u3059\u308b \u73fe\u5728\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3067\u884c\u308f\u308c\u305f\u3059\u3079\u3066\u306e\u5909\u66f4\u3092\u5143\u306b\u623b\u3059\u5834\u5408\u306f\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002 pythonCopy code\u3053\u308c\u306b\u3088\u308a\u3001\u30b3\u30df\u30c3\u30c8\u3055\u308c\u3066\u3044\u306a\u3044\u3059\u3079\u3066\u306e\u5909\u66f4\u304c\u53d6\u308a\u6d88\u3055\u308c\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u7d42\u4e86\u3057\u307e\u3059\u3002<code>rollback<\/code><code>session.rollback()<\/code><\/li>\n\n\n\n<li><strong>\u624b\u52d5\u7d42\u4e86<\/strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u624b\u52d5\u3067\u958b\u59cb\u3057\u305f\u5834\u5408\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e or \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u7d42\u4e86\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002 python\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b\u304b\u3001python\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b\u304b<code>transaction<\/code><code>commit<\/code><code>rollback<\/code><code>transaction.commit()&nbsp;<\/code><code>transaction.rollback()<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b46\u8a18<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5165\u308c\u5b50\u306b\u306a\u3063\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3: SQLAlchemy \u306f<strong>\u5165\u308c\u5b50\u306b\u306a\u3063\u305f<\/strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u3042\u308b\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u5225\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5185\u3067\u958b\u59cb\u3067\u304d\u307e\u3059\u3002 \u3053\u308c\u306f\u901a\u5e38\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u4ecb\u3057\u3066\u884c\u308f\u308c\u307e\u3059\u3002<code>begin_nested<\/code><\/li>\n\n\n\n<li>\u30bb\u30fc\u30d6\u30dd\u30a4\u30f3\u30c8:\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5185\u3067\u8907\u6570\u306e\u30bb\u30fc\u30d6\u30dd\u30a4\u30f3\u30c8(<strong>\u30bb\u30fc\u30d6\u30dd\u30a4\u30f3\u30c8<\/strong>)\u3092\u8a2d\u5b9a\u3057\u3066\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u5931\u6557\u3057\u305f\u5834\u5408\u306b\u7279\u5b9a\u306e\u72b6\u614b\u306b\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3059\u308b\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52d5\u30ed\u30fc\u30eb\u30d0\u30c3\u30af<\/strong>: \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u5207\u65ad\u306a\u3069\u3001\u5834\u5408\u306b\u3088\u3063\u3066\u306f\u3001SQLAlchemy \u306f\u73fe\u5728\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u81ea\u52d5\u7684\u306b\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb<\/strong>: \u4e00\u822c\u306b\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u72ec\u7acb\u6027\u3092\u78ba\u4fdd\u3059\u308b\u305f\u3081\u306b\u3001\u5404\u8981\u6c42\u306e\u65b0\u3057\u3044\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u5fc5\u8981\u3067\u3059\u3002<code>Session<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u30c7\u30fc\u30bf\u306e<\/strong>\u4e00\u8cab\u6027: \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u4e00\u9023\u306e\u64cd\u4f5c\u304c\u3059\u3079\u3066\u306e\u64cd\u4f5c\u3092\u6210\u529f\u307e\u305f\u306f\u5931\u6557\u3055\u305b\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u4e00\u8cab\u6027\u3092\u4fdd\u8a3c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30a8\u30e9\u30fc\u56de\u5fa9<\/strong>: \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u30a8\u30e9\u30fc\u3084\u969c\u5bb3\u304b\u3089\u7c21\u5358\u306b\u56de\u5fa9\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u540c\u6642\u5b9f\u884c\u5236\u5fa1<\/strong>: \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306f\u3001\u8907\u6570\u306e\u30e6\u30fc\u30b6\u30fc\u304c\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u540c\u6642\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u304d\u306b\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u306e\u3042\u308b\u554f\u984c\u3092\u7ba1\u7406\u3059\u308b\u305f\u3081\u306b\u3082\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>SQLAlchemy \u3067\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u958b\u59cb\u304a\u3088\u3073\u7d42\u4e86\u3059\u308b\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3092\u3088\u308a\u52b9\u679c\u7684\u306b\u7ba1\u7406\u3057\u3001\u30c7\u30fc\u30bf\u306e\u4e00\u8cab\u6027\u3068\u6574\u5408\u6027\u3092\u78ba\u4fdd\u3067\u304d\u307e\u3059\u3002 \u3053\u308c\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3092\u4f34\u3046\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u975e\u5e38\u306b\u91cd\u8981\u306a\u90e8\u5206\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406\u3068\u30ed\u30fc\u30eb\u30d0\u30c3\u30afChatGPT\u306e\u30a8\u30e9\u30fc\u51e6\u7406\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u7ba1\u7406: \u30a8\u30e9\u30fc\u51e6\u7406\u3068\u30ed\u30fc\u30eb\u30d0\u30c3\u30af<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u30a8\u30e9\u30fc\u51e6\u7406\u3068\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3068\u306f\u4f55\u3067\u3059\u304b?<\/h4>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u30a8\u30e9\u30fc\u51e6\u7406\u3068\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u304c 2 \u3064\u306e\u91cd\u8981\u306a\u6982\u5ff5\u3067\u3059\u3002 \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5185\u3067\u4e00\u9023\u306e\u64cd\u4f5c\u3092\u5b9f\u884c\u3059\u308b\u3068\u304d\u306b\u3001\u3044\u305a\u308c\u304b\u306e\u64cd\u4f5c\u304c\u5931\u6557\u3057\u305f\u5834\u5408\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u4e00\u8cab\u6027\u3068\u6574\u5408\u6027\u3092\u4fdd\u8a3c\u3059\u308b\u305f\u3081\u306b\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5168\u4f53\u3092\u30ed\u30fc\u30eb\u30d0\u30c3\u30af (\u5143\u306b\u623b\u3059) \u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u30a8\u30e9\u30fc\u51e6\u7406<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4f8b\u5916<strong>\u3092\u30ad\u30e3\u30c3\u30c1\u3059\u308b<\/strong>&nbsp;\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\u3068\u304d\u306f\u3001try-except \u30d6\u30ed\u30c3\u30af\u3092\u4f7f\u7528\u3057\u3066\u3001\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u306e\u3042\u308b\u4f8b\u5916\u3092\u30ad\u30e3\u30c3\u30c1\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 pythonCopy code<code>try: new_user = User(name='John', email='john@example.com') session.add(new_user) session.commit() except Exception as e: print(f\"An error occurred: {e}\")<\/code><\/li>\n\n\n\n<li>\u30ab\u30b9\u30bf\u30e0\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8: \u30ad\u30e3\u30c3\u30c1\u3057\u305f\u4f8b\u5916\u306e\u7a2e\u985e\u306b\u57fa\u3065\u3044\u3066\u3001\u3088\u308a\u5177\u4f53\u7684\u306a<strong>\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8<\/strong>\u3092\u51fa\u529b\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>from sqlalchemy.exc import IntegrityError, OperationalError try: # ... (transaction code) except IntegrityError: print(\"Integrity error, possibly due to a constraint violation.\") except OperationalError: print(\"Operational error, possibly due to database connection issues.\") except Exception as e: print(f\"An unknown error occurred: {e}\")<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u64cd\u4f5c<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>\u30ed\u30fc\u30eb\u30d0\u30c3\u30af<\/code>&nbsp;\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b<\/strong>&nbsp;\u4f8b\u5916\u3092\u30ad\u30e3\u30c3\u30c1\u3057\u305f\u3089\u3001RollBack \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001\u30b3\u30df\u30c3\u30c8\u3055\u308c\u3066\u3044\u306a\u3044\u3059\u3079\u3066\u306e\u5909\u66f4\u3092\u5143\u306b\u623b\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 pythonCopy code<code>rollback<\/code><code>try: # ... (transaction code) session.commit() except Exception as e: session.rollback() print(f\"An error occurred: {e}. Transaction rolled back.\")<\/code><\/li>\n\n\n\n<li><strong><code>with<\/code>&nbsp;\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u4f7f\u7528\u3059\u308b\u3068<\/strong>\u3001\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3068\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u81ea\u52d5\u7684\u306b\u51e6\u7406\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002 pythonCopy code \u3053\u306e\u5834\u5408\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5185\u306e\u3044\u305a\u308c\u304b\u306e\u64cd\u4f5c\u304c\u5931\u6557\u3059\u308b\u3068\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u81ea\u52d5\u7684\u306b\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u64cd\u4f5c\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<code>with<\/code><code>Session<\/code><code>begin<\/code><code>with session.begin(): # ... (transaction code)&nbsp;<\/code><code>Session<\/code><\/li>\n\n\n\n<li>\u30cd\u30b9\u30c8\u3055\u308c\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068\u30bb\u30fc\u30d6\u30dd\u30a4\u30f3\u30c8 \u3088\u308a\u8907\u96d1\u306a\u30b7\u30ca\u30ea\u30aa\u3067\u306f\u3001<strong>\u30cd\u30b9\u30c8\u3055\u308c\u305f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068\u30bb\u30fc\u30d6\u30dd\u30a4\u30f3\u30c8<\/strong>\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002 pythonCopy code<code>try: with session.begin_nested(): # ... (nested transaction code) except Exception as e: session.rollback() print(f\"An error occurred in the nested transaction: {e}\")<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b46\u8a18<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8907\u6570\u306e\u30ed\u30fc\u30eb\u30d0\u30c3\u30af<\/strong>: \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3067\u30e1\u30bd\u30c3\u30c9\u3092\u8907\u6570\u56de\u547c\u3073\u51fa\u3059\u3068\u3001\u6700\u521d\u306e\u547c\u3073\u51fa\u3057\u306e\u307f\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<code>rollback<\/code><\/li>\n\n\n\n<li><strong>\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u9589\u3058\u308b<\/strong>: \u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u5f8c\u3001\u7279\u306b\u5b9f\u884c\u6642\u9593\u306e\u9577\u3044\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u9589\u3058\u308b\u304b\u30ea\u30bb\u30c3\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u304b\u3092\u691c\u8a0e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<code>Session<\/code><\/li>\n\n\n\n<li><strong>\u30ed\u30b0\u8a18\u9332<\/strong>: \u30c7\u30d0\u30c3\u30b0\u3068\u76e3\u67fb\u306b\u5f79\u7acb\u3064\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u6210\u529f\u3068\u5931\u6557\u3092\u8ffd\u8de1\u3059\u308b\u305f\u3081\u306b\u30ed\u30b0\u8a18\u9332\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u30c7\u30fc\u30bf\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3<\/strong>: \u30a8\u30e9\u30fc\u3092\u9069\u5207\u306b\u51e6\u7406\u3057\u3001\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3067\u3001\u30a8\u30e9\u30fc\u3084\u969c\u5bb3\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u3067\u3082\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u72b6\u614b\u306e\u4e00\u8cab\u6027\u3092\u4fdd\u3064\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u4fdd\u5b88\u6027<\/strong>: \u69cb\u9020\u5316\u3055\u308c\u305f\u30a8\u30e9\u30fc\u51e6\u7406\u3068\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u306e\u30e1\u30ab\u30cb\u30ba\u30e0\u3092\u4f7f\u7528\u3057\u3066\u3001\u30b3\u30fc\u30c9\u306e\u4fdd\u5b88\u3068\u30c7\u30d0\u30c3\u30b0\u3092\u5bb9\u6613\u306b\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>SQLAlchemy \u3067\u30a8\u30e9\u30fc\u51e6\u7406\u3068\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u884c\u3046\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u7279\u306b\u30a8\u30e9\u30fc\u3084\u4f8b\u5916\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306b\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u3088\u308a\u52b9\u679c\u7684\u306b\u7ba1\u7406\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002 \u3053\u308c\u306b\u3088\u308a\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u5805\u7262\u6027\u304c\u5411\u4e0a\u3059\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u30c7\u30fc\u30bf\u306e\u4e00\u8cab\u6027\u3068\u6574\u5408\u6027\u3082\u4fdd\u8a3c\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u71b1\u5fc3\u306a\u8aad\u307f\u8fbc\u307fChatGPT\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u6700\u9069\u5316:\u71b1\u5fc3\u306a\u8aad\u307f\u8fbc\u307f<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u71b1\u5fc3\u306a\u8aad\u307f\u8fbc\u307f\u3068\u306f\u4f55\u3067\u3059\u304b?<\/h4>\n\n\n\n<p>SQLAlchemy \u3067\u306f\u3001\u4e00\u62ec\u8aad\u307f\u8fbc\u307f\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u3066\u3044\u308b\u4ed6\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30d7\u30ea\u30ed\u30fc\u30c9\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30af\u30a8\u30ea\u306e\u6570\u3092\u6e1b\u3089\u3059\u305f\u3081\u306e\u6700\u9069\u5316\u624b\u6cd5\u3067\u3059\u3002 \u3053\u308c\u306f\u30011 \u3064\u306e\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u8907\u6570\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u53d6\u5f97\u3057\u3001\u305d\u308c\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u3066\u3044\u308b\u4ed6\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u30ec\u30b3\u30fc\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001\u591a\u6570\u306e\u8ffd\u52a0\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30af\u30a8\u30ea\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b N+&lt;&gt; \u30af\u30a8\u30ea\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u3088\u304f\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u306a\u65b9\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>joinedload \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068<\/strong>\u3001\u7d50\u5408\u30af\u30a8\u30ea\u5185\u306e\u95a2\u9023\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4e8b\u524d\u306b\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 pythonCopy code \u3053\u3053\u3067\u306f\u3001\u30e6\u30fc\u30b6\u30fc\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u3066\u3044\u308b\u3059\u3079\u3066\u306e\u6ce8\u6587\u304c 1 \u3064\u306e\u30af\u30a8\u30ea\u306b\u8aad\u307f\u8fbc\u307e\u308c\u307e\u3059\u3002<code>joinedload<\/code><code>from sqlalchemy.orm import joinedload query = session.query(User).options(joinedload(User.orders)).filter(User.id == 1)<\/code><\/li>\n\n\n\n<li><strong>SubqueryLoad<\/strong>&nbsp;\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u5225\u306e\u30b5\u30d6\u30af\u30a8\u30ea\u306b\u30d7\u30ea\u30ed\u30fc\u30c9\u3067\u304d\u307e\u3059\u3002 pythonCopy code \u3053\u3053\u3067\u306f\u3001\u30e6\u30fc\u30b6\u30fc\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u3066\u3044\u308b\u3059\u3079\u3066\u306e\u6ce8\u6587\u304c\u5225\u306e\u30b5\u30d6\u30af\u30a8\u30ea\u306b\u8aad\u307f\u8fbc\u307e\u308c\u307e\u3059\u3002<code>subqueryload<\/code><code>from sqlalchemy.orm import subqueryload query = session.query(User).options(subqueryload(User.orders)).filter(User.id == 1)<\/code><\/li>\n\n\n\n<li><strong>selectinload \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068<\/strong>\u3001\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30af\u30a8\u30ea\u306b\u30d7\u30ea\u30ed\u30fc\u30c9\u3067\u304d\u307e\u3059\u3002 pythonCopy code \u3053\u3053\u3067\u306f\u3001\u30e6\u30fc\u30b6\u30fc\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u3066\u3044\u308b\u3059\u3079\u3066\u306e\u6ce8\u6587\u304c 1 \u3064\u306e\u30af\u30a8\u30ea\u306b\u8aad\u307f\u8fbc\u307e\u308c\u307e\u3059\u3002<code>selectinload<\/code><code>IN<\/code><code>from sqlalchemy.orm import selectinload query = session.query(User).options(selectinload(User.orders)).filter(User.id == 1)&nbsp;<\/code><code>IN<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u30a4\u30fc\u30ac\u30fc\u30ed\u30fc\u30c9\u3092\u4f7f\u7528\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3088\u3044\u3067\u3059\u304b?<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4e00\u5bfe\u591a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u306a\u3069\u306e\u5358\u4e00\u306e\u95a2\u9023\u4ed8\u3051<strong>\u306e\u5834\u5408\u306f<\/strong>\u3001\u5358\u306b\u3001\u3001\u307e\u305f\u306f \u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>joinedload<\/code><code>subqueryload<\/code><code>selectinload<\/code><code>query = session.query(User).options(joinedload(User.orders))<\/code><\/li>\n\n\n\n<li>\u8907\u6570\u30ec\u30d9\u30eb\u306e\u95a2\u9023\u4ed8\u3051 1 \u5bfe\u591a\u306e 1 \u5bfe\u591a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u306a\u3069\u3001<strong>\u8907\u6570\u30ec\u30d9\u30eb\u306e\u95a2\u9023\u4ed8\u3051<\/strong>\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>joinedload<\/code><code>nested<\/code><code>from sqlalchemy.orm import joinedload query = session.query(User).options(joinedload(User.orders, Order.items))<\/code><\/li>\n\n\n\n<li>\u52d5\u7684\u8aad\u307f\u8fbc\u307f\u7279\u5b9a\u306e\u6761\u4ef6\u306b\u57fa\u3065\u3044\u3066<strong>\u8aad\u307f\u8fbc\u307f\u65b9\u6cd5\u3092\u52d5\u7684<\/strong>\u306b\u9078\u629e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u5b9f\u884c\u6642\u306b\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>load<\/code><code>from sqlalchemy.orm import load_only query = session.query(User).options(load_only(\"name\", \"email\"))<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u5411\u4e0a<\/strong>: \u4e00\u62ec\u8aad\u307f\u8fbc\u307f\u306b\u3088\u308a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30af\u30a8\u30ea\u306e\u6570\u304c\u6e1b\u308b\u3053\u3068\u3067\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u5927\u5e45\u306b\u5411\u4e0a\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u7c21\u7565\u5316\u3055\u308c\u305f<\/strong>\u30b3\u30fc\u30c9: \u4e00\u62ec\u8aad\u307f\u8fbc\u307f\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u5fc5\u8981\u306a\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u3092 1 \u3064\u306e\u30af\u30a8\u30ea\u3067\u53d6\u5f97\u3067\u304d\u308b\u305f\u3081\u3001\u5f8c\u7d9a\u306e\u30c7\u30fc\u30bf\u51e6\u7406\u30b3\u30fc\u30c9\u304c\u7c21\u7d20\u5316\u3055\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u67d4\u8edf\u6027<\/strong>: SQLAlchemy\u306b\u306f\u3055\u307e\u3056\u307e\u306aEager\u8aad\u307f\u8fbc\u307f\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u7528\u610f\u3055\u308c\u3066\u304a\u308a\u3001\u7279\u5b9a\u306e\u30cb\u30fc\u30ba\u306b\u6700\u9069\u306a\u8aad\u307f\u8fbc\u307f\u6226\u7565\u3092\u9078\u629e\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4e00\u62ec\u8aad\u307f\u8fbc\u307f\u3092\u7406\u89e3\u3057\u3066\u9069\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30af\u30a8\u30ea\u3092\u6700\u9069\u5316\u3057\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u5fdc\u7b54\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u3053\u308c\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306e\u975e\u5e38\u306b\u91cd\u8981\u306a\u5074\u9762\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u30af\u30a8\u30ea\u6700\u9069\u5316ChatGPT\u306e\u6027\u80fd\u6700\u9069\u5316\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u6700\u9069\u5316: \u30af\u30a8\u30ea\u306e\u6700\u9069\u5316<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u30af\u30a8\u30ea\u306e\u6700\u9069\u5316\u3068\u306f<\/h4>\n\n\n\n<p>\u30af\u30a8\u30ea\u306e\u6700\u9069\u5316\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9 \u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306e\u91cd\u8981\u306a\u5074\u9762\u3067\u3042\u308a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30af\u30a8\u30ea\u306e\u8907\u96d1\u3055\u3068\u6570\u3092\u6e1b\u3089\u3059\u3053\u3068\u3067\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002 SQLAlchemy \u306b\u306f\u3001\u30af\u30a8\u30ea\u3092\u6700\u9069\u5316\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3067\u304d\u308b\u3044\u304f\u3064\u304b\u306e\u30e1\u30bd\u30c3\u30c9\u3068\u624b\u6cd5\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u306a\u65b9\u6cd5\u3068\u30c6\u30af\u30cb\u30c3\u30af<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9078\u629e\u7684\u8aad\u307f\u8fbc\u307f\u3067\u306f<\/strong>\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u5168\u4f53\u3067\u306f\u306a\u304f\u3001\u5b9f\u969b\u306b\u5fc5\u8981\u306a\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u307f\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002 pythonCopy code<code>load_only<\/code><code>from sqlalchemy.orm import load_only query = session.query(User).options(load_only(\"name\", \"email\"))<\/code><\/li>\n\n\n\n<li>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c6\u30fc\u30d6\u30eb\u306e\u30ad\u30fc \u30d5\u30a3\u30fc\u30eb\u30c9\u306b<strong>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9<\/strong>\u3092\u4f5c\u6210\u3057\u3001\u30af\u30a8\u30ea\u3092\u9ad8\u901f\u5316\u3057\u307e\u3059\u3002 pythonCopy code<code>from sqlalchemy import Index index = Index('my_index', User.name)<\/code><\/li>\n\n\n\n<li>\u4e00\u62ec<strong>\u64cd\u4f5c\u3067\u306f<\/strong>\u3001\u307e\u305f\u306f\u4e00\u62ec\u633f\u5165\u307e\u305f\u306f\u4e00\u62ec\u66f4\u65b0\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002 pythonCopy code<code>bulk_insert_mappings<\/code><code>bulk_update_mappings<\/code><code>session.bulk_insert_mappings(User, [{'name': 'user1'}, {'name': 'user2'}])<\/code><\/li>\n\n\n\n<li>\u30af\u30a8\u30ea \u30ad\u30e3\u30c3\u30b7\u30e5<strong>\u3067\u306f\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u4f7f\u7528\u3057\u3066<\/strong>\u983b\u7e41\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u30af\u30a8\u30ea\u7d50\u679c\u3092\u683c\u7d0d\u3057\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30a2\u30af\u30bb\u30b9\u3092\u6e1b\u3089\u3057\u307e\u3059\u3002 pythonCopy code<code>from sqlalchemy_cache import Cache cache = Cache(session) query = session.query(User).options(cache.cache())<\/code><\/li>\n\n\n\n<li>\u30b5\u30d6\u30af\u30a8\u30ea\u3068\u7d50\u5408\u3092\u6700\u9069\u5316\u3059\u308b \u8907\u96d1\u306a<strong>\u30b5\u30d6\u30af\u30a8\u30ea\u3068\u7d50\u5408<\/strong>\u306e\u4f7f\u7528\u3092\u907f\u3051\u308b\u304b\u3001\u30af\u30a8\u30ea \u30d7\u30e9\u30f3\u306e\u5206\u6790\u3068\u305d\u308c\u306b\u5fdc\u3058\u3066\u6700\u9069\u5316\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002 \u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30fc\u3059\u308b<code>EXPLAIN<\/code><code>EXPLAIN SELECT * FROM users WHERE name = 'John';<\/code><\/li>\n\n\n\n<li>\u30da\u30fc\u30b8\u30f3\u30b0\u3067\u306f\u3001\u30af\u30a8\u30ea\u306e<strong>\u30da\u30fc\u30b8\u30f3\u30b0<\/strong>\u306b and \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001\u30af\u30a8\u30ea\u306b\u3088\u3063\u3066\u8fd4\u3055\u308c\u308b\u30c7\u30fc\u30bf\u306e\u91cf\u3092\u6e1b\u3089\u3057\u307e\u3059\u3002 pythonCopy code<code>limit<\/code><code>offset<\/code><code>query = session.query(User).limit(10).offset(20)<\/code><\/li>\n\n\n\n<li>\u901a\u5e38\u3001\u524d\u8005\u306e\u65b9\u304c\u9ad8\u901f\u3067\u3042\u308b\u305f\u3081\u3001\u6761\u4ef6\u306b\u4e00\u81f4\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3059\u308b\u3060\u3051\u3067\u3088\u3044\u5834\u5408\u306f\u3001<strong>COUNT \u306e\u4ee3\u308f\u308a\u306b EXISTS \u3092\u4f7f\u7528\u3057\u307e\u3059<\/strong>\u3002 pythonCopy code<code>EXISTS<\/code><code>COUNT<\/code><code>from sqlalchemy import exists query = session.query(exists().where(User.name == 'John'))<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9<strong>\u306e\u5411\u4e0a<\/strong>: \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30af\u30a8\u30ea\u306e\u6570\u3068\u8907\u96d1\u3055\u3092\u8efd\u6e1b\u3059\u308b\u3053\u3068\u3067\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30ea\u30bd\u30fc\u30b9\u4f7f\u7528\u91cf\u306e\u524a\u6e1b<\/strong>: \u6700\u9069\u5316\u3055\u308c\u305f\u30af\u30a8\u30ea\u306f\u3001\u901a\u5e38\u3001\u4f7f\u7528\u3059\u308b CPU \u3068\u30e1\u30e2\u30ea\u306e\u30ea\u30bd\u30fc\u30b9\u304c\u5c11\u306a\u304f\u306a\u308a\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u5fdc\u7b54\u6027\u306e\u5411\u4e0a<\/strong>: \u30af\u30a8\u30ea\u306e\u6700\u9069\u5316\u306b\u3088\u308a\u3001\u591a\u304f\u306e\u5834\u5408\u3001\u30da\u30fc\u30b8\u306e\u8aad\u307f\u8fbc\u307f\u304c\u901f\u304f\u306a\u308a\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u30e6\u30fc\u30b6\u30fc \u30a8\u30af\u30b9\u30da\u30ea\u30a8\u30f3\u30b9\u304c\u30b9\u30e0\u30fc\u30ba\u306b\u306a\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u30af\u30a8\u30ea\u6700\u9069\u5316\u624b\u6cd5\u3092\u7406\u89e3\u3057\u3066\u9069\u7528\u3059\u308b\u3053\u3068\u3067\u3001SQLAlchemy \u3068\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u3001\u3088\u308a\u9ad8\u901f\u3067\u4fe1\u983c\u6027\u306e\u9ad8\u3044\u30b5\u30fc\u30d3\u30b9\u3092\u63d0\u4f9b\u3067\u304d\u307e\u3059\u3002 \u3053\u308c\u306f\u3001\u3042\u3089\u3086\u308b\u30b5\u30a4\u30ba\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u3068\u3063\u3066\u91cd\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30a4\u30f3\u30c8\u30ed\u30b9\u30da\u30af\u30b7\u30e7\u30f3\u3068\u751f\u6210\u3055\u308c\u305f\u81ea\u52d5\u751f\u6210\u3055\u308c\u305fCREATE\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8ChatGPT\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30a4\u30f3\u30c8\u30ed\u30b9\u30da\u30af\u30b7\u30e7\u30f3\u3068\u751f\u6210\u3055\u308c\u305f\u81ea\u52d5\u751f\u6210 CREATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30a4\u30f3\u30c8\u30ed\u30b9\u30da\u30af\u30b7\u30e7\u30f3\u3068\u306f\u4f55\u3067\u3059\u304b?<\/h4>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30a4\u30f3\u30c8\u30ed\u30b9\u30da\u30af\u30b7\u30e7\u30f3\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30ad\u30fc\u30de\u3001\u30c6\u30fc\u30d6\u30eb\u3001\u30d5\u30a3\u30fc\u30eb\u30c9\u3001\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u306a\u3069\u306b\u95a2\u3059\u308b\u8a73\u7d30\u60c5\u5831\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308b\u624b\u6cd5\u3067\u3059\u3002 SQLAlchemy \u3067\u306f\u3001\u30a4\u30f3\u30c8\u30ed\u30b9\u30da\u30af\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3057\u3066\u3001\u65e2\u5b58\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u69cb\u9020\u306b\u4e00\u81f4\u3059\u308b\u3088\u3046\u306b Python \u30e2\u30c7\u30eb\u3092\u81ea\u52d5\u7684\u306b\u4f5c\u6210\u307e\u305f\u306f\u66f4\u65b0\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u306a\u65b9\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30ea\u30d5\u30ec\u30af\u30b7\u30e7\u30f3\u306f\u3001<\/strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30a4\u30f3\u30c8\u30ed\u30b9\u30da\u30af\u30b7\u30e7\u30f3\u306e\u305f\u3081\u306b SQLAlchemy \u3067\u4f7f\u7528\u3055\u308c\u308b\u4e3b\u8981\u306a\u65b9\u6cd5\u3067\u3059\u3002 \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u60c5\u5831\u3092\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 pythonCopy code \u3053\u306e\u3088\u3046\u306b\u3057\u3066\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5185\u306e\u3059\u3079\u3066\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u95a2\u3059\u308b\u60c5\u5831\u304c\u542b\u307e\u308c\u307e\u3059\u3002<code>MetaData<\/code><code>reflect<\/code><code>from sqlalchemy import MetaData meta = MetaData() meta.reflect(bind=engine)&nbsp;<\/code><code>meta<\/code><\/li>\n\n\n\n<li><strong>\u81ea\u52d5\u30ed\u30fc\u30c9<\/strong>&nbsp;\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3059\u308b\u3068\u304d\u306b\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u5217\u60c5\u5831\u3092\u81ea\u52d5\u7684\u306b\u8aad\u307f\u8fbc\u3080\u3088\u3046\u306b\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002 pythonCopy code<code>autoload=True<\/code><code>from sqlalchemy import Table my_table = Table('my_table', meta, autoload=True, autoload_with=engine)<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">CREATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u81ea\u52d5\u7684\u306b\u751f\u6210\u3059\u308b<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>create_all<\/code>\u3092\u4f7f\u7528\u3059\u308b<\/strong>&nbsp;\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3057\u305f\u3089\u3001CREATE \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066 CREATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u81ea\u52d5\u7684\u306b\u751f\u6210\u3057\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002 pythonCopy code \u3053\u308c\u306b\u3088\u308a\u3001\u30af\u30e9\u30b9\u3067\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3059\u3079\u3066\u306e\u30e2\u30c7\u30eb\u306b\u5bfe\u3057\u3066\u9069\u5207\u306a CREATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u304c\u751f\u6210\u3055\u308c\u3001\u305d\u308c\u3089\u3092\u5b9f\u884c\u3057\u3066\u30c6\u30fc\u30d6\u30eb\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002<code>create_all<\/code><code>Base.metadata.create_all(engine)&nbsp;<\/code><code>Base<\/code><\/li>\n\n\n\n<li>SQL \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8<strong>\u306e\u751f\u6210 \u751f\u6210\u3055\u308c\u305f SQL CREATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8<\/strong>\u3092\u5b9f\u884c\u305b\u305a\u306b\u8868\u793a\u3059\u308b\u5834\u5408\u306f\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002 pythonCopy \u30b3\u30fc\u30c9: \u3053\u308c\u306b\u3088\u308a\u3001\u30e2\u30c7\u30eb\u306b\u5bfe\u5fdc\u3059\u308b CREATE TABLE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<code>string<\/code><code>from sqlalchemy.schema import CreateTable print(CreateTable(User.__table__).compile(engine))&nbsp;<\/code><code>User<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30e2\u30c7\u30eb\u3068\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u540c\u671f\u3059\u308b: \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30a4\u30f3\u30c8\u30ed\u30b9\u30da\u30af\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001Python&nbsp;<strong>\u30e2\u30c7\u30eb\u3068<\/strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30ad\u30fc\u30de\u304c\u5e38\u306b\u540c\u671f\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u624b\u4f5c\u696d\u306e\u524a\u6e1b<\/strong>: CREATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u81ea\u52d5\u7684\u306b\u751f\u6210\u3059\u308b\u3068\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30b9\u30ad\u30fc\u30de\u3092\u624b\u52d5\u3067\u4f5c\u6210\u304a\u3088\u3073\u4fdd\u5b88\u3059\u308b\u52b4\u529b\u304c\u8efd\u6e1b\u3055\u308c\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30a8\u30e9\u30fc<strong>\u304c\u5c11\u306a\u3044<\/strong>: \u591a\u304f\u306e\u5834\u5408\u3001\u81ea\u52d5\u751f\u6210\u3055\u308c\u305f SQL \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u306f\u624b\u52d5\u3067\u8a18\u8ff0\u3059\u308b\u3088\u308a\u3082\u6b63\u78ba\u3067\u3042\u308b\u305f\u3081\u3001\u30a8\u30e9\u30fc\u306e\u53ef\u80fd\u6027\u304c\u4f4e\u304f\u306a\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30a4\u30f3\u30c8\u30ed\u30b9\u30da\u30af\u30b7\u30e7\u30f3\u3068 CREATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u306e\u81ea\u52d5\u751f\u6210\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30fb\u30b9\u30ad\u30fc\u30de\u3092\u3088\u308a\u52b9\u679c\u7684\u306b\u7ba1\u7406\u304a\u3088\u3073\u4fdd\u5b88\u3067\u304d\u308b\u3068\u540c\u6642\u306b\u3001\u624b\u4f5c\u696d\u3084\u30a8\u30e9\u30fc\u3092\u6e1b\u3089\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u3053\u308c\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u8a2d\u8a08\u3068\u4fdd\u5b88\u306b\u975e\u5e38\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/chat.openai.com\/_next\/image?url=https%3A%2F%2Flh3.googleusercontent.com%2Fa%2FAAcHTtdQvKTfcfwR0bXrfLjIN0Vmoln5pVObYXE2hRFieJsISok%3Ds96-c&amp;w=96&amp;q=75\" alt=\"User\"\/><\/figure>\n\n\n\n<p>ChatGPT\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3068\u9ad8\u5ea6\u306a\u30c8\u30d4\u30c3\u30af\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u8aac\u660e\u3057\u3066\u304f\u3060\u3055\u3044<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30d9\u30b9\u30c8 \u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3068\u9ad8\u5ea6\u306a\u30c8\u30d4\u30c3\u30af: \u30bb\u30ad\u30e5\u30ea\u30c6\u30a3<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3068\u306f<\/h4>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306b\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u305d\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u4e0d\u6b63\u30a2\u30af\u30bb\u30b9\u3084\u4e0d\u6b63\u64cd\u4f5c\u304b\u3089\u4fdd\u8b77\u3059\u308b\u305f\u3081\u306e\u5bfe\u7b56\u304c\u542b\u307e\u308c\u307e\u3059\u3002 SQLAlchemy \u307e\u305f\u306f\u305d\u306e\u4ed6\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30a2\u30af\u30bb\u30b9 \u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3092\u8003\u616e\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u306a\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u5bfe\u7b56<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>SQL \u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u4fdd\u8b77<\/strong>SQLAlchemy\u306e\u30af\u30a8\u30ea\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306f\u3001\u3059\u3079\u3066\u306eSQL\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u81ea\u52d5\u7684\u306b\u30a8\u30b9\u30b1\u30fc\u30d7\u3059\u308b\u3088\u3046\u306b\u8a2d\u8a08\u3055\u308c\u3066\u304a\u308a\u3001SQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u306e\u30ea\u30b9\u30af\u3092\u8efd\u6e1b\u3057\u307e\u3059\u3002 \u751f\u306e SQL \u30af\u30a8\u30ea\u306e\u4f7f\u7528\u306f\u907f\u3051\u3066\u304f\u3060\u3055\u3044\u3002 pythonCopy code<code># \u63a8\u8350 session.query(User).filter_by(name='John') # \u4e0d\u63a8\u8350 session.execute(\"SELECT * FROM users WHERE name = 'John';\")<\/code><\/li>\n\n\n\n<li><strong>\u6700\u5c0f\u7279\u6a29\u306e\u539f\u5247\u3067\u306f<\/strong>\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30e6\u30fc\u30b6\u30fc\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u308b\u6a29\u9650\u306f\u3067\u304d\u308b\u3060\u3051\u5c11\u306a\u304f\u306a\u308a\u307e\u3059\u3002 \u305f\u3068\u3048\u3070\u3001\u30b5\u30fc\u30d3\u30b9\u306b\u30c7\u30fc\u30bf\u306e\u8aad\u307f\u53d6\u308a\u306e\u307f\u304c\u5fc5\u8981\u306a\u5834\u5408\u306f\u3001\u66f8\u304d\u8fbc\u307f\u30a2\u30af\u30bb\u30b9\u8a31\u53ef\u3092\u4ed8\u4e0e\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002<\/li>\n\n\n\n<li><strong>\u74b0\u5883\u5909\u6570\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u8cc7\u683c\u60c5\u5831\u3092\u683c\u7d0d<\/strong>\u3057\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u8cc7\u683c\u60c5\u5831\u3092\u30b3\u30fc\u30c9\u306b\u30cf\u30fc\u30c9\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002 \u74b0\u5883\u5909\u6570\u307e\u305f\u306f\u30ad\u30fc\u7ba1\u7406\u30b5\u30fc\u30d3\u30b9\u3092\u4f7f\u7528\u3057\u3066\u3001\u3053\u306e\u60c5\u5831\u3092\u4fdd\u5b58\u3057\u307e\u3059\u3002 pythonCopy code<code>import os db_url = os.environ.get('DATABASE_URL')<\/code><\/li>\n\n\n\n<li><strong>\u30c7\u30fc\u30bf\u6697\u53f7\u5316<\/strong>\u3067\u306f\u3001SSL\/TLS \u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u306e\u901a\u4fe1\u3092\u6697\u53f7\u5316\u3057\u307e\u3059\u3002 \u307e\u305f\u3001\u30d5\u30a3\u30fc\u30eb\u30c9\u30ec\u30d9\u30eb\u306e\u6697\u53f7\u5316\u3092\u4f7f\u7528\u3057\u3066\u6a5f\u5bc6\u30c7\u30fc\u30bf\u3092\u4fdd\u8b77\u3059\u308b\u3053\u3068\u3082\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/li>\n\n\n\n<li>\u76e3\u67fb\u3068\u30ed\u30b0<strong>\u8a18\u9332\u306b\u3088\u308a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u76e3\u67fb\u30ed\u30b0<\/strong>\u3067\u3001\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3 \u30a4\u30d9\u30f3\u30c8\u306e\u767a\u751f\u3092\u8ffd\u8de1\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u5165\u529b\u691c\u8a3c\u3067\u306f\u3001\u30e6\u30fc\u30b6\u30fc<strong>\u5165\u529b<\/strong>\u304c\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30af\u30a8\u30ea\u306b\u4f7f\u7528\u3055\u308c\u308b\u524d\u306b\u3001\u5e38\u306b\u691c\u8a3c\u3068\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u304c\u884c\u308f\u308c\u307e\u3059\u3002 pythonCopy code<code>from validate_email_address import validate_email if validate_email(user_email): session.query(User).filter_by(email=user_email)<\/code><\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ad8\u5ea6\u306a\u30c6\u30fc\u30de<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u884c\u30ec\u30d9\u30eb\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306f\u3001<\/strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30ec\u30d9\u30eb\u3067\u884c\u30ec\u30d9\u30eb\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3092\u5b9f\u88c5\u3057\u3066\u3001\u3069\u306e\u30e6\u30fc\u30b6\u30fc\u304c\u3069\u306e\u30c7\u30fc\u30bf\u884c\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u304b\u3092\u5236\u9650\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>ORM \u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3 \u30e2\u30fc\u30c9\u3092\u4f7f\u7528\u3059\u308b<\/strong>SQLAlchemy \u306b\u306f\u3001\u52d5\u7684\u30d5\u30a3\u30fc\u30eb\u30c9\u540d\u306a\u3069\u3001\u5b89\u5168\u3067\u306a\u3044\u53ef\u80fd\u6027\u306e\u3042\u308b\u7279\u5b9a\u306e\u64cd\u4f5c\u3092\u7121\u52b9\u306b\u3059\u308b &#8220;\u30bb\u30fc\u30d5 \u30e2\u30fc\u30c9&#8221; \u304c\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30d5\u30a1\u30a4\u30a2\u30a6\u30a9\u30fc\u30eb: \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30d5\u30a1\u30a4\u30a2\u30a6\u30a9\u30fc\u30eb\u3092\u4f7f\u7528\u3057\u3066\u3001<strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9<\/strong>\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b IP \u30a2\u30c9\u30ec\u30b9\u3092\u5236\u9650\u3057\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u5b9a\u671f\u7684\u306a<\/strong>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u76e3\u67fb \u5b9a\u671f\u7684\u306a\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u76e3\u67fb\u3092\u5b9f\u65bd\u3057\u3066\u3001\u6f5c\u5728\u7684\u306a\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306e\u8106\u5f31\u6027\u3092\u691c\u51fa\u3057\u3066\u4fee\u6b63\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fa1\u5024<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u30c7\u30fc\u30bf\u4fdd\u8b77<\/strong>: \u3053\u308c\u3089\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306e\u30d9\u30b9\u30c8 \u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u3092\u3088\u308a\u52b9\u679c\u7684\u306b\u4fdd\u8b77\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li><strong>\u30b3\u30f3\u30d7\u30e9\u30a4\u30a2\u30f3\u30b9<\/strong>: \u591a\u304f\u306e\u696d\u754c\u3084\u5730\u57df\u306b\u306f\u53b3\u683c\u306a\u30c7\u30fc\u30bf\u4fdd\u8b77\u3068\u30d7\u30e9\u30a4\u30d0\u30b7\u30fc\u898f\u5236\u304c\u3042\u308a\u3001\u3053\u308c\u3089\u306e\u30d9\u30b9\u30c8 \u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u306b\u5f93\u3046\u3053\u3068\u3067\u30b3\u30f3\u30d7\u30e9\u30a4\u30a2\u30f3\u30b9\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li>\u30ea\u30b9\u30af<strong>\u306e\u8efd\u6e1b<\/strong>: \u4e0d\u6b63\u30a2\u30af\u30bb\u30b9\u3084\u4e0d\u6b63\u64cd\u4f5c\u3092\u9632\u6b62\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u4fb5\u5bb3\u3084\u305d\u306e\u4ed6\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3 \u30a4\u30f3\u30b7\u30c7\u30f3\u30c8\u306e\u30ea\u30b9\u30af\u3092\u5927\u5e45\u306b\u8efd\u6e1b\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306b\u95a2\u3059\u308b\u3053\u308c\u3089\u306e\u30d9\u30b9\u30c8 \u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3068\u9ad8\u5ea6\u306a\u30c8\u30d4\u30c3\u30af\u3092\u7406\u89e3\u3057\u3066\u9069\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u3092\u4fdd\u8b77\u3067\u304d\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u5168\u4f53\u7684\u306a\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002 \u3053\u308c\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u958b\u767a\u3068\u4fdd\u5b88\u306b\u304a\u3044\u3066\u975e\u5e38\u306b\u91cd\u8981\u306a\u90e8\u5206\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30a6\u30a7\u30d6\u30b5\u30a4\u30c8\u3078\u306e\u30ea\u30f3\u30af\u3092\u305f\u3069\u3063\u3066\u3001SQL &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"footnotes":""},"categories":[23],"tags":[],"class_list":["post-2853","post","type-post","status-publish","format-standard","hentry","category-material"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts\/2853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2853"}],"version-history":[{"count":1,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts\/2853\/revisions"}],"predecessor-version":[{"id":2854,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts\/2853\/revisions\/2854"}],"wp:attachment":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}