{"id":42036,"date":"2024-12-01T08:42:38","date_gmt":"2024-12-01T00:42:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42036\/"},"modified":"2024-12-01T08:42:38","modified_gmt":"2024-12-01T00:42:38","slug":"gorm-database-firsttablestruct-id-%e6%b2%a1%e6%9c%89deleted_at-is-null","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42036\/","title":{"rendered":"GORM Database.First(&#038;tableStruct, id) \u6ca1\u6709deleted_at IS NULL"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>GORM Database.First(&amp;tableStruct, id) \u6ca1\u6709deleted_at IS NULL<\/span><\/p>\n<h1>GORM Database.First(&amp;tableStruct, id) \u6ca1\u6709deleted_at IS NULL<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 13:03:26<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGORM Database.First(&amp;tableStruct, id) \u6ca1\u6709deleted_at IS NULL\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5f53<code>tableStruct<\/code>\u91cc\u9762\u6709<code>gorm.Model<\/code>\u65f6\uff0c\u5982\u4f55\u901a\u8fc7<code>id<\/code>\u83b7\u53d6\u7b2c\u4e00\u6761\u8bb0\u5f55\u800c\u4e0d\u9700\u8981gorm\u81ea\u52a8\u6dfb\u52a0<code>deleted_at IS NULL<\/code>\uff1f\u6709\u8fd9\u6837\u7684\u529f\u80fd\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u539f\u59cb\u67e5\u8be2<\/p>\n<pre>db.raw(\"select * from table limit 1\").scan(&amp;variable)<\/pre>\n<p>\u770b\u5b8c\u6e90\u7801\uff0c\u6709<code>unscoped<\/code>\u65b9\u6cd5\u9632\u6b62\u6dfb\u52a0<code>deleted_at is null<\/code><\/p>\n<pre>Database.Unscoped().First(&amp;tableStruct, id)<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aGORM Database.First(&amp;tableStruct, id) \u6ca1\u6709deleted_at IS NULL\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; GORM Database.First(&amp;tableStruct, id) \u6ca1\u6709deleted_at IS NULL GORM Database.First(&amp;tableStruct, id) \u6ca1\u6709deleted_at IS NULL \u6765\u6e90\uff1astackoverflow 2024-04-22 13:03:26 0\u6d4f\u89c8 \u6536\u85cf \u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300aGORM Database.First(&amp;tableStruct, id) \u6ca1\u6709deleted_at IS NULL\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9Golang\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01 \u95ee\u9898\u5185\u5bb9 \u5f53tableStruct\u91cc\u9762\u6709gorm.Model\u65f6\uff0c\u5982\u4f55\u901a\u8fc7id\u83b7\u53d6\u7b2c\u4e00\u6761\u8bb0\u5f55\u800c\u4e0d\u9700\u8981gorm\u81ea\u52a8\u6dfb\u52a0deleted_at IS NULL\uff1f\u6709\u8fd9\u6837\u7684\u529f\u80fd\u5417\uff1f \u89e3\u51b3\u65b9\u6848 \u60a8\u53ef\u4ee5\u4f7f\u7528\u539f\u59cb\u67e5\u8be2 db.raw(&#8220;select * from table limit 1&#8221;).scan(&amp;variable) \u770b\u5b8c\u6e90\u7801\uff0c\u6709unscoped\u65b9\u6cd5\u9632\u6b62\u6dfb\u52a0deleted_at is null Database.Unscoped().First(&amp;tableStruct, id) \u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aGORM Database.First(&amp;tableStruct, id) \u6ca1\u6709deleted_at IS NULL\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42036","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42036","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=42036"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42036\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}