{"id":865,"date":"2024-11-07T09:35:49","date_gmt":"2024-11-07T01:35:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/865\/"},"modified":"2024-11-07T09:35:49","modified_gmt":"2024-11-07T01:35:49","slug":"gorm-raw-%e6%9f%a5%e8%af%a2-unsupported-destination-%e9%94%99%e8%af%af%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3-select-database-%e6%9f%a5%e8%af%a2%e7%bb%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/865\/","title":{"rendered":"GORM Raw \u67e5\u8be2 \u201cunsupported destination\u201d \u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3 \u201cselect database()\u201d \u67e5\u8be2\u7ed3\u679c\u6620\u5c04\u95ee\u9898\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1048\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173020572478010.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173020572478010.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173020572478010-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173020572478010-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173020572478010-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"GORM Raw \u67e5\u8be2 \u201cunsupported destination\u201d \u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3 \u201cselect database()\u201d \u67e5\u8be2\u7ed3\u679c\u6620\u5c04\u95ee\u9898\uff1f\u63d2\u56fe\" alt=\"GORM Raw \u67e5\u8be2 \u201cunsupported destination\u201d \u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3 \u201cselect database()\u201d \u67e5\u8be2\u7ed3\u679c\u6620\u5c04\u95ee\u9898\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>gorm raw\u67e5\u8be2\u201cunsupported destination, should be slice or struct\u201d\u9519\u8bef\uff1a<\/strong><\/p>\n<p>\u5728\u4f7f\u7528gorm\u8fdb\u884c\u539f\u751fsql\u67e5\u8be2\u65f6\uff0c\u5982\u679c\u51fa\u73b0\u201cunsupported destination, should be slice or struct\u201d\u9519\u8bef\uff0c\u5176\u6839\u672c\u539f\u56e0\u5728\u4e8egorm\u7684raw\u65b9\u6cd5\u672c\u8d28\u4e0a\u5c06\u67e5\u8be2\u7ed3\u679c\u6620\u5c04\u5230\u7ed3\u6784\u4f53\u6216\u5207\u7247\u4e2d\uff0c\u800c\u50cf\u201cselect database()\u201d\u8fd9\u7c7b\u67e5\u8be2\u7ed3\u679c\u4e0e\u8868\u7ed3\u6784\u65e0\u5173\u3002<\/p>\n<p>\u5b98\u65b9\u6587\u6863\u63d0\u4f9b\u7684\u793a\u4f8b\uff08var age int db.raw(&#8230;).scan(&amp;age)\uff09\u4e4b\u6240\u4ee5\u6210\u529f\uff0c\u662f\u56e0\u4e3aage\u662f\u4e00\u4e2a\u660e\u786e\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5b83\u7b26\u5408gorm\u7684\u6620\u5c04\u673a\u5236\u3002\u7136\u800c\uff0c\u50cf\u201cselect database()\u201d\u8fd9\u7c7b\u67e5\u8be2\u7ed3\u679c\u65e0\u6cd5\u76f4\u63a5\u6620\u5c04\u5230\u7ed3\u6784\u4f53\u6216\u5207\u7247\u3002<\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u4f7f\u7528go\u539f\u751f\u7684<em>sql.db\u7c7b\u578b\u7684queryrow\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7gorm.db\u7ed3\u6784\u4f53\u7684db()\u65b9\u6cd5\u83b7\u53d6<\/em>sql.db\u7c7b\u578b\u3002<\/p>\n<p>\u4fee\u6539\u540e\u7684\u6b63\u786e\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>func showCurrentDb() {\n    var database string\n    persistence.GetDB().DB().QueryRow(\"select database()\").Scan(&amp;database)\n    fmt.Println(\"database \", database)\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fGORM Raw \u67e5\u8be2 \u201cunsupported destination\u201d \u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3 \u201cselect database()\u201d \u67e5\u8be2\u7ed3\u679c\u6620\u5c04\u95ee\u9898\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>gorm raw\u67e5\u8be2\u201cunsupported destination, should be slice or struct\u201d\u9519\u8bef\uff1a \u5728\u4f7f\u7528gorm\u8fdb\u884c\u539f\u751fsql\u67e5\u8be2\u65f6\uff0c\u5982\u679c\u51fa\u73b0\u201cunsupported destination, should be slice or struct\u201d\u9519\u8bef\uff0c\u5176\u6839\u672c\u539f\u56e0\u5728\u4e8egorm\u7684raw\u65b9\u6cd5\u672c\u8d28\u4e0a\u5c06\u67e5\u8be2\u7ed3\u679c\u6620\u5c04\u5230\u7ed3\u6784\u4f53\u6216\u5207\u7247\u4e2d\uff0c\u800c\u50cf\u201cselect database()\u201d\u8fd9\u7c7b\u67e5\u8be2\u7ed3\u679c\u4e0e\u8868\u7ed3\u6784\u65e0\u5173\u3002 \u5b98\u65b9\u6587\u6863\u63d0\u4f9b\u7684\u793a\u4f8b\uff08var age int db.raw(&#8230;).scan(&amp;age)\uff09\u4e4b\u6240\u4ee5\u6210\u529f\uff0c\u662f\u56e0\u4e3aage\u662f\u4e00\u4e2a\u660e\u786e\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5b83\u7b26\u5408gorm\u7684\u6620\u5c04\u673a\u5236\u3002\u7136\u800c\uff0c\u50cf\u201cselect database()\u201d\u8fd9\u7c7b\u67e5\u8be2\u7ed3\u679c\u65e0\u6cd5\u76f4\u63a5\u6620\u5c04\u5230\u7ed3\u6784\u4f53\u6216\u5207\u7247\u3002 \u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u4f7f\u7528go\u539f\u751f\u7684sql.db\u7c7b\u578b\u7684queryrow\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7gorm.db\u7ed3\u6784\u4f53\u7684db()\u65b9\u6cd5\u83b7\u53d6sql.db\u7c7b\u578b\u3002 \u4fee\u6539\u540e\u7684\u6b63\u786e\u4ee3\u7801\u5982\u4e0b\uff1a func showCurrentDb() { var database string persistence.GetDB().DB().QueryRow(&#8220;select database()&#8221;).Scan(&amp;database) fmt.Println(&#8220;database &#8220;, database) } \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662fGORM Raw \u67e5\u8be2 \u201cunsupported destination\u201d \u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3 \u201cselect database()\u201d \u67e5\u8be2\u7ed3\u679c\u6620\u5c04\u95ee\u9898\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-865","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=865"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/865\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}