GORM many2many help

polaris · · 740 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I can&#39;t work out the best way to add an association to a model. I have the following structs</p> <pre><code>type Beer struct { ID uint `json:&#34;id&#34;` Name string `json:&#34;name&#34; gorm:&#34;not null;&#34; sql:&#34;unique&#34;` Description string `json:&#34;description&#34; gorm:&#34;not null;&#34;` ImageURL string `json:&#34;image_url&#34;` AlcoholContent float64 `json:&#34;alcohol_content, default:0&#34;` Featured bool `json:&#34;featured&#34;` BrewStart time.Time `json:&#34;brew_start&#34;` BrewEnd time.Time `json:&#34;brew_end&#34;` Brewers []Brewer `gorm:&#34;many2many:beer_brewers&#34; json:&#34;brewers&#34;` } type Brewer struct { ID uint `json:&#34;id&#34;` FirstName string `json:&#34;first_name&#34;` LastName string `json:&#34;last_name&#34;` Title string `json:&#34;title&#34;` Featured bool `json:&#34;featured&#34;` Beers []Beer `gorm:&#34;many2many:beer_brewers&#34; json:&#34;beers&#34;` } </code></pre> <p>Below is an example of data i have seeded the DB with</p> <pre><code>Beer{ Name: &#34;some pale ale&#34;, Description: &#34;a description of some pale ale&#34;, ImageURL: &#34;http://via.placeholder.com/350x150&#34;, AlcoholContent: 4.5, Featured: false, BrewStart: utils.ParseTime(&#34;30-10-2017 13:00 (AEDT)&#34;), BrewEnd: utils.ParseTime(&#34;14-11-2017 13:00 (AEDT)&#34;), Brewers: []Brewer{ Brewer{FirstName: &#34;john&#34;, LastName: &#34;smith&#34;, Title: &#34;bottle shaker&#34;, Featured: false}, Brewer{FirstName: &#34;joe&#34;, LastName: &#34;bloggs&#34;, Title: &#34;bottle maker&#34;, Featured: true}, }, }, Beer{ Name: &#34;some lager&#34;, Description: &#34;a description of some pale ale&#34;, ImageURL: &#34;http://via.placeholder.com/350x150&#34;, AlcoholContent: 4.5, Featured: false, BrewStart: utils.ParseTime(&#34;30-10-2017 13:00 (AEDT)&#34;), BrewEnd: utils.ParseTime(&#34;14-11-2017 13:00 (AEDT)&#34;), Brewers: []Brewer{ Brewer{FirstName: &#34;john&#34;, LastName: &#34;smith&#34;, Title: &#34;bottle shaker&#34;, Featured: false}, Brewer{FirstName: &#34;joe&#34;, LastName: &#34;bloggs&#34;, Title: &#34;bottle maker&#34;, Featured: true}, }, }, </code></pre> <p>However the above creates duplicate Brewers in the Brewer table. My question is, what is the best way to reference a Brewer that already exists but not create another Brewer item in the Brewer table?..and also what is the best way to Append a new Brewer into a Beer collection?</p> <p>Thanks, Justin</p> <hr/>**评论:**<br/><br/>hobbified: <pre><p>If you don&#39;t want it to think that a record is new and needs to be inserted, you need to set the ID field in the struct to the ID of the existing row (usually you do this by loading the row from the DB in the first place, but not necessarily. <code>FirstOrCreate</code> can be helpful here.)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

740 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传