{"id":207483,"date":"2025-07-08T13:42:23","date_gmt":"2025-07-08T05:42:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207483\/"},"modified":"2025-07-08T13:42:23","modified_gmt":"2025-07-08T05:42:23","slug":"go-sqlboiler%ef%bc%9a%e6%9f%a5%e8%af%a2%e5%85%b3%e7%b3%bb%e5%b9%b6%e8%bf%94%e5%9b%9e","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207483\/","title":{"rendered":"Go SQLBoiler\uff1a\u67e5\u8be2\u5173\u7cfb\u5e76\u8fd4\u56de"},"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>Go SQLBoiler\uff1a\u67e5\u8be2\u5173\u7cfb\u5e76\u8fd4\u56de<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 23:00:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo SQLBoiler\uff1a\u67e5\u8be2\u5173\u7cfb\u5e76\u8fd4\u56de\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u4f7f\u7528 sqlboiler\uff0c\u5982\u4f55\u67e5\u8be2\u5173\u7cfb\u5e76\u8fd4\u56de\uff1f\u6211\u6709\u4e24\u4e2a\u5355\u72ec\u7684\u6570\u636e\u5e93\u8868\u7528\u4e8e\u4ea7\u54c1\u53ca\u5176\u4ef7\u683c\/\u91d1\u94b1\u3002\u6211\u60f3\u67e5\u8be2\u4e24\u8005\u5e76\u5c06\u5b83\u4eec\u4e00\u8d77\u8fd4\u56de\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u4f8b\u5982 <code>getall<\/code> \u8bf7\u6c42\u3002<\/p>\n<p>\u8fd9\u662f\u521d\u59cb\u67b6\u6784\u6587\u4ef6\u3002<\/p>\n<pre>-- schema.sql\n\ndrop table if exists products cascade;\ndrop table if exists money;\n\ncreate table products (\n    id serial not null primary key,\n    user_id int not null,\n    name varchar(255) not null,\n    description text not null,\n    created_at timestamptz not null default now(),\n    updated_at timestamptz not null default now(),\n    deleted_at timestamptz\n);\n\ncreate table money (\n    product_id int not null primary key,\n    currency_code text not null default 'usd',\n    units int not null,\n    nanos int not null,\n    created_at timestamptz not null default now(),\n    updated_at timestamptz not null default now(),\n\n    foreign key (product_id) references products (id) on delete cascade\n);\n\ncreate unique index idx_products_name\non products (name);\n\ninsert into products (user_id, name, description) values (1, 'something', 'this is something.');\ninsert into products (user_id, name, description) values (2, 'anything', 'this is anything.');\n\ninsert into money (product_id, currency_code, units, nanos) values (1, 'gbp', 450, 75);\ninsert into money (product_id, currency_code, units, nanos) values (2, 'cad', 9, 20);\n<\/pre>\n<pre>...\n\nfunc (s *server) index(context.context, *pb.empty) (*pb.indexproductresponse, error) {\n    db := initpg()\n    defer db.close()\n\n    \/\/ make a slice of product type from generated protobuf\n    products := make([]*pb.product, 0)\n\n    \/\/ get all products (without price)\n    ps, err := models.products().all(context.background(), db)\n    logandreport(&amp;pb.indexproductresponse{}, err)\n\n    \/\/ how do i query each product's price from money table?\n    \/\/ or\n    \/\/ how do i query all money and assign to each related products?\n\n    \/\/ fmt.println(\"@ ps here &gt;\", ps)\n\n    \/\/ append queried products into products slice to return\n    for _, p := range ps {\n        products = append(products,\n            &amp;pb.product{\n                id:          int32(p.id),\n                name:        p.name,\n                description: p.description,\n                \/\/ i need to return\/assign price information here.\n                \/\/ i can do like this with gorm.\n                \/\/\n                \/\/ price: &amp;pb.money{\n                \/\/  currencycode: p.price.currencycode,\n                \/\/  units: p.price.units,\n                \/\/  nanos: p.price.nanos,\n                \/\/ },\n                userid:      int32(p.userid),\n            })\n    }\n\n    return &amp;pb.indexproductresponse{products: products}, nil\n}\n\n...\n<\/pre>\n<p>\u8fd9\u662f\u6765\u81ea\u521b\u5efa\u4ea7\u54c1\uff0c\u4f46\u4ea7\u54c1\u5217\u8868\u4e2d\u7684\u9884\u671f graphql \u7ed3\u679c\u51e0\u4e4e\u76f8\u540c\uff1a<\/p>\n<pre>{\n  \"data\": {\n    \"products\": [\n      {\n        \"id\": \"1\",\n        \"name\": \"something\",\n        \"description\": \"this is something.\",\n        \"price\": {\n          \"currencycode\": \"usd\",\n          \"units\": 9,\n          \"nanos\": 93\n        },\n        \"userid\": \"1\"\n      },\n      {\n        \"id\": \"2\",\n        \"name\": \"anything\",\n        \"description\": \"this is anything.\",\n        \"price\": {\n          \"currencycode\": \"usd\",\n          \"units\": 24,\n          \"nanos\": 56\n        },\n        \"userid\": \"1\"\n      }\n    ]\n  },\n...\n}\n...<\/pre>\n<p>\u8fd9\u662f\u4ea7\u54c1\u670d\u52a1\u7684protobuf\u6587\u4ef6\u3002<\/p>\n<pre>service ProductService {\n  rpc Index(Empty) returns (IndexProductResponse) {}\n}\n\nmessage Money {\n  \/\/ The 3-letter currency code defined in ISO 4217.\n  string currency_code = 1;\n\n  \/\/ The whole units of the amount.\n  \/\/ For example if `currencyCode` is `\"USD\"`, then 1 unit is one US dollar.\n  int64 units = 2;\n\n  \/\/ Number of nano (10^-9) units of the amount.\n  \/\/ The value must be between -999,999,999 and +999,999,999 inclusive.\n  \/\/ If `units` is positive, `nanos` must be positive or zero.\n  \/\/ If `units` is zero, `nanos` can be positive, zero, or negative.\n  \/\/ If `units` is negative, `nanos` must be negative or zero.\n  \/\/ For example $-1.75 is represented as `units`=-1 and `nanos`=-750,000,000.\n  int32 nanos = 3;\n}\n\nmessage Product {\n  int32 id = 1;\n  string name = 2;\n  string description = 3;\n  Money price = 4;\n  int32 user_id = 5;\n}\n\nmessage IndexProductResponse {\n  repeated Product products = 1;\n}\n<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u60f3\u8981\u52a0\u8f7d\u4e3b\u8981\u5bf9\u8c61 product \u53ca\u5176\u6240\u6709\u76f8\u5173\u7684 money \u884c\u5417\uff1f<\/p>\n<p>\u8fd9\u5e94\u8be5\u53ef\u4ee5\u505a\u5230\uff1a<\/p>\n<pre>products, err :=\n    models.products(\n        qm.load(models.productrels.productmoneys)\n    ).all(context.background(), db)<\/pre>\n<p>\u8fd9\u5c06\u751f\u6210\u5e76\u6267\u884c\u4e24\u4e2a sql \u67e5\u8be2\u3002\u7b2c\u4e00\u4e2a\u67e5\u8be2\u5e94\u8be5\u662f<\/p>\n<pre>select * from \"products\"<\/pre>\n<p>\u5e76\u5c06\u68c0\u7d22\u6240\u6709\u4ea7\u54c1\u3002\u7b2c\u4e8c\u4e2a\u67e5\u8be2\u5e94\u8be5\u7c7b\u4f3c\u4e8e<\/p>\n<pre>select * from \"money\" where (\"money\".\"product_id\" in ($1,$2,$3))<\/pre>\n<p>\u5e76\u5c06\u6536\u56de\u6240\u6709\u4ea7\u54c1\u7684\u6240\u6709\u8d44\u91d1\u3002 \uff08<code>in<\/code> \u5b50\u53e5\u7684\u5177\u4f53\u5185\u5bb9\u5c06\u53d6\u51b3\u4e8e\u9000\u56de\u7684\u4ea7\u54c1\u6570\u91cf\u3002\uff09<\/p>\n<p>\u7136\u540e\u60a8\u5e94\u8be5\u80fd\u591f\u8bbf\u95ee\u76f8\u5173\u884c\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>products[0].R.ProductMoneys<\/pre>\n<p>\u6ce8\u610f\uff1a<code>productrels<\/code> \u548c\/\u6216 <code>productmoneys<\/code> \u53ef\u80fd\u4e0d\u662f sqlboiler \u751f\u6210\u7684\u786e\u5207\u540d\u79f0\u3002\u6211\u4e0d\u5f97\u4e0d\u5728\u8fd9\u91cc\u731c\u6d4b\u3002\u786e\u4fdd\u68c0\u67e5\u751f\u6210\u7684\u4ee3\u7801\u4ee5\u83b7\u5f97\u6b63\u786e\u7684\u540d\u79f0\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aGo SQLBoiler\uff1a\u67e5\u8be2\u5173\u7cfb\u5e76\u8fd4\u56de\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207483","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207483"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207483\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}