<p>How do you all keep your models organized? I recently created a db-to-code schema/model generator and am saving all in the same "model" package. The problem I encountered is from generated table-column pairs, ex:</p>
<pre><code>table -> account
column -> id // AccountID
column -> typeId // **AccountTypeID**
table -> account_type
column -> id // **AccountTypeID**
</code></pre>
<p>The issue is that I get generated column names AccountTypeID for both tables.</p>
<p>There are definitely going to happen other cases of generated columns having the same name, so I think I'd have to end up giving each model a separate package.</p>
<hr/>**评论:**<br/><br/>TiberiusAbadon: <pre><p>I'm seen a similar (maybe not) issue and solution in sqlx. </p>
<p><a href="http://jmoiron.github.io/sqlx/#advancedScanning" rel="nofollow">http://jmoiron.github.io/sqlx/#advancedScanning</a></p>
<p>Nothing wrong with asking the user to resolve possible ambiguities. With struct tags maybe?</p></pre>drink_with_me_to_day: <pre><p>I am generating "static" models, no tags or dependency on reflect. </p></pre>driusan: <pre><p>Without seeing the code that you're generating, I don't see where the conceptual problem is. It sounds like you're making everything too flat. If I were writing something to represent those tables from scratch instead of generating it, I'd expect it to be something like:</p>
<pre><code>type AccountTableRow struct {
Id int
TypeId *AccountTypeTableRow
}
type AccountTypeTableRow struct {
Id int
}
</code></pre>
<p>There's no conflict there..</p></pre>Akkifokkusu: <pre><p>Instead of having a <code>models</code> package, have you thought about organizing your code in a different way? <a href="https://medium.com/@msandin/strategies-for-organizing-code-2c9d690b6f33#.p75r9thd1" rel="nofollow">This article</a> is a pretty good overview, and to me having a <code>models</code> package is code smell that falls under the "By Kind" category.</p>
<p>I'd have packages for <code>account</code> and <code>accounttype</code>that encapsulate the logic related to dealing with each one.</p></pre>drink_with_me_to_day: <pre><p>That was a good read. Having separate packages for each model might work.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传