{"id":3282,"date":"2025-02-14T06:49:40","date_gmt":"2025-02-13T21:49:40","guid":{"rendered":"https:\/\/nobunobu1717.site\/?p=3282"},"modified":"2025-02-14T06:49:40","modified_gmt":"2025-02-13T21:49:40","slug":"sqlalchemy%e3%81%a7%e3%83%a2%e3%83%87%e3%83%ab%e3%82%92%e7%b6%99%e6%89%bf%e3%81%97%e3%81%a6%e7%94%a8%e9%80%94%e3%82%92%e5%88%86%e3%81%91%e3%81%9f%e3%82%af%e3%83%a9%e3%82%b9%e3%82%92%e4%bd%9c%e3%82%8b","status":"publish","type":"post","link":"https:\/\/nobunobu1717.site\/?p=3282","title":{"rendered":"SQLAlchemy\u3067\u30e2\u30c7\u30eb\u3092\u7d99\u627f\u3057\u3066\u7528\u9014\u3092\u5206\u3051\u305f\u30af\u30e9\u30b9\u3092\u4f5c\u308b"},"content":{"rendered":"<h2>\u6982\u8981<\/h2>\n<p>SQLAlchemy2.0\u3067\u3001\u7d99\u627f\u306b\u3088\u3063\u3066\u8907\u6570\u306e\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u3001\u7528\u9014\u306b\u5fdc\u3058\u3066\u5fc5\u8981\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8ffd\u52a0\u3055\u305b\u308b\u65b9\u6cd5\u306e\u30e1\u30e2\u3002<\/p>\n<p>\u3053\u306e\u65b9\u6cd5\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u4e00\u3064\u306e\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u5185\u306b\u4e00\u90e8\u306e\u30b1\u30fc\u30b9\u3067\u306e\u307f\u4f7f\u7528\u3059\u308b\u30d5\u30a3\u30fc\u30eb\u30c9\u304c\u5897\u52a0\u3057\u3066\u80a5\u5927\u5316\u3059\u308b\u3053\u3068\u3092\u9632\u3052\u308b\u3002<\/p>\n<h2><span style=\"color: #1a1a1a;\"><span style=\"caret-color: #1a1a1a; font-size: 20px; background-color: #f3f3f3;\"><b>\u8aac\u660e<\/b><\/span><\/span><\/h2>\n<h3>\u80cc\u666f<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306a\u66f8\u7c4d\u30ec\u30d3\u30e5\u30fc\u30e2\u30c7\u30eb\u3068\u3001\u30e6\u30fc\u30b6\u30fc\u30e2\u30c7\u30eb\u304c\u3042\u308b\u3068\u3059\u308b\u30021:1\u306e\u95a2\u4fc2\u3067\u66f8\u7c4d\u30ec\u30d3\u30e5\u30fc\u306f\u30e6\u30fc\u30b6\u30fcID\u306e\u307f\u3092\u4fdd\u6301\u3059\u308b\u3002<\/p>\n<p>\u30b3\u30e1\u30f3\u30c8\u30a2\u30a6\u30c8\u3055\u308c\u305fuser\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u3088\u3046\u306b\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u5b9f\u614b\u3092\u6301\u305f\u305b\u3001\u53c2\u7167\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\u3057\u304b\u3057\u306a\u304c\u3089\u3001id\u3060\u3051\u3067\u306f\u306a\u304f\u3001UserDTO\u81ea\u4f53\u304c\u6b32\u3057\u3044\u30b1\u30fc\u30b9\u3068\u6b32\u3057\u304f\u306a\u3044\u30b1\u30fc\u30b9\u304c\u3042\u308b\u306e\u3067\u4e21\u65b9\u3092\u6301\u305f\u305b\u308b\u306e\u306f\u6c17\u6301\u3061\u60aa\u3044\u3002<\/p>\n<pre class=\"lang:python decode:true\">from sqlalchemy.orm import relationship\r\n\r\nclass BookReviewDTO(Base):\r\n    __tablename__ = \"book_reviews\"\r\n    __table_args__ = {\"comment\": \"\u66f8\u7c4d\u30ec\u30d3\u30e5\u30fc\"}\r\n\r\n    review_id: Mapped[UUID] = mapped_column(Uuid, primary_key=True, comment=\"\u4e3b\u30ad\u30fc\")\r\n    content: Mapped[str] = mapped_column(String(length=10000), comment=\"\u30ec\u30d3\u30e5\u30fc\u672c\u6587\")\r\n    user_id: Mapped[UUID] = mapped_column(Uuid, ForeignKey(\"users.user_id\"))\r\n    # user: Mapped[UserDTO] = relationship() # \u30e6\u30fc\u30b6\u30e2\u30c7\u30eb\u306e\u5b9f\u614b\u3082\u6301\u305f\u305b\u3089\u308c\u308b\u3002 \r\n\r\n\r\nclass UserDTO(DeletableBase):\r\n    __tablename__ = \"users\"\r\n    __table_args__ = {\"comment\": \"\u30e6\u30fc\u30b6\u30fc\"}\r\n\r\n    user_id: Mapped[UUID] = mapped_column(Uuid, primary_key=True, comment=\"\u4e3b\u30ad\u30fc\")\r\n    name: Mapped[str] = mapped_column(String(length=30), nullable=False, comment=\"\u30e6\u30fc\u30b6\u30fc\u540d\")\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>\u89e3\u6c7a\u65b9\u6cd5<\/h3>\n<p>\u305d\u3053\u3067\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u5143\u306e\u30e2\u30c7\u30eb\u3092\u7d99\u627f\u3057\u3066\u3001\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u5b9f\u614b\u3092\u6301\u3064\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u5b9a\u7fa9\u3059\u308b\u3002<\/p>\n<pre class=\"lang:python decode:true\">class BookReviewExtendDTO(BookReviewDTO):\r\n    ex_user: Mapped[UserDTO] = relationship()\r\n<\/pre>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306bUserDTO\u306e\u5b9f\u614b\u306b\u30a2\u30af\u30bb\u30b9\u3057\u305f\u3044\u5834\u5408\u306b\u306f\u3001\u7d99\u627f\u3057\u305fBookReviewExtendDTO\u3067select\u3092\u884c\u3046\u3002<\/p>\n<p>\u30d9\u30fc\u30b9\u30af\u30e9\u30b9\u306eBookReviewDTO\u306b\u5074\u306b\u3001\u4f7f\u3046\u5834\u5408\u3068\u4f7f\u308f\u306a\u3044\u5834\u5408\u304c\u3042\u308b\u3001\u4f59\u8a08\u306a\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u3092\u907f\u3051\u308b\u3053\u3068\u304c\u304d\u3066\u898b\u901a\u3057\u304c\u826f\u304f\u306a\u3063\u305f\u3002<\/p>\n<pre class=\"lang:python decode:true\">        stmt = (\r\n            select(BookReviewExtendDTO)\r\n            .options(\r\n                contains_eager(UserDTO),\r\n            )\r\n        )\r\n        results = self._session.scalars(stmt).unique().all()<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 SQLAlchemy2.0\u3067\u3001\u7d99\u627f\u306b\u3088\u3063\u3066\u8907\u6570\u306e\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u3001\u7528\u9014\u306b\u5fdc\u3058\u3066\u5fc5\u8981\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8ffd\u52a0\u3055\u305b\u308b\u65b9\u6cd5\u306e\u30e1\u30e2\u3002 \u3053\u306e\u65b9\u6cd5\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u4e00\u3064\u306e\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u5185\u306b\u4e00\u90e8\u306e\u30b1\u30fc\u30b9 &#8230; <\/p>\n","protected":false},"author":1,"featured_media":1361,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34],"tags":[],"_links":{"self":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/3282"}],"collection":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3282"}],"version-history":[{"count":2,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/3282\/revisions"}],"predecessor-version":[{"id":3284,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/3282\/revisions\/3284"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/media\/1361"}],"wp:attachment":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}