In Gorm, is it possible to define an extra column on a join table?

xuanbao · · 363 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I realise this isn&#39;t really a Q&amp;A sub, but I asked on Stack Overflow and IRC and didn&#39;t get any responses, so I figured this couldn&#39;t hurt.</p> <p>I want a database that looks like this:</p> <pre><code>groups table id | name ----|--------- 1 | &#34;Family&#34; 2 | &#34;Friends&#34; shows table id | name ----|--------- 1 | &#34;Seinfeld&#34; 2 | &#34;The X-Files&#34; groups_shows_link group_id | show_id | rewatch ---------|---------|--------- 1 | 1 | 1508764915 2 | 1 | 1508522911 </code></pre> <p>That is, I want to put an extra field <code>rewatch</code> on the join table, and then be able to ask Gorm for all of group 1&#39;s shows, <em>or</em> all of group 1&#39;s shows <code>where rewatch &gt; ?</code>.</p> <p>I&#39;m not sure what to call this to search for it, nor can I find information about such a thing in Gorm&#39;s documentation. What should I do in this situation?</p> <p>Right now my <code>Group</code> struct has <code>Shows []Show</code>, and that creates the join table just fine, but I can&#39;t see a way to add a column to it. It occurred to me to create an entirely new <code>GroupShowsLink</code> struct with a <code>rewatch int</code> field, and say that each group has 1 of those, and each of of those has 1 show, but that seems pretty hacky and like it&#39;d be messy to search through?</p> <hr/>**评论:**<br/><br/>gopher2018: <pre><p>I don&#39;t think Gorm supports adding columns to join tables.</p> <p>I recomment you to create a dedicated struct to the join table. Then you have the flexibility to add column and write any SQL you want.</p></pre>

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

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