[Beginner] Need help understanding models with foreign keys in go (using sqlx)

blov · · 518 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey folks!</p> <p>I&#39;m a junior web dev used to working in the python/Django space. I&#39;m super interested in Go and am currently working on a simple web-based directory project to try and get a bit of experience with the language.</p> <p>My first major hurdle is moving from the extremely magic heavy ORM model to a more &#34;raw&#34; way of working with the database. </p> <p>Let&#39;s say I have the following structs:</p> <pre><code>type Location struct { ID uint64 Name string } type Person struct { ID uint64 FirstName string LastName string } </code></pre> <p>and Person has-a Location. What is the most idiomatic way to link these models? I&#39;ve read about embedding anonymous structs, but it&#39;s not quite clear to me how to set these up.</p> <p>Thanks in advance and if I can clarify I&#39;m happy to post more code/info </p> <hr/>**评论:**<br/><br/>adelowo: <pre><p>Struct embedding is the way to go.. At the barest level, it is just &#34;inheritance&#34;..</p> <p>As far as I know, sqlx can do this... You tell sqlx to map the relationship on &#34;loading from the db&#34; by struct tags (reflection ish)..</p> <p>So basically, you convert your person struct to this</p> <pre><code>type Person struct { ID uint64 `db:&#34;id&#34;` FirstName string `db:&#34;first_name&#34;` LastName string `db:&#34;last_name&#34;` Location Location `db:&#34;location&#34;` } </code></pre> <p>Edit --&gt; I forgot to mention you have to make use of <code>StructScan</code> to get the data into the <code>User</code> Struct</p> <p>Edit -&gt; <a href="/u/ChristophBerger" rel="nofollow">/u/ChristophBerger</a> has a link to what embedding really is</p></pre>ChristophBerger: <pre><p>Agreed, this would be the usual way of linking a location to a person (in case of a 1-1 relationship). </p> <p>Just one minor side node: The term &#34;embedding&#34; is usually only <a href="https://golang.org/ref/spec#Struct_types" rel="nofollow">used for anonymous fields</a>, sometimes also called embedded fileds. The Location field above is just a normal field in a struct.</p></pre>timberwolf5922: <pre><p>Just wanted to follow up here: Is the above (Location as a field on Person) only useful for 1-1? The relationship I&#39;m trying to represent is many-to-one (Many Person structs can have the same Location)</p></pre>ChristophBerger: <pre><p>Sorry, my fault. You are right, it is of course an M-1 relationship between Person and Location.</p></pre>adelowo: <pre><p>Thanks for the correction.</p></pre>Wrathbun: <pre><p>Another noob here, Ive been struggling with this as well. Struct embedding seems to be the recommended option but it feels kinda hacky to me to represent database relationships this way? Is there any alternative patterns for this?</p> <p>Also just to clarify, using OPs example, if I have a &#34;location_id&#34; foreign key on my person table, is sqlx smart enough to unmarshall &#34;location_id&#34; into the id field on the Location embedded struct even though it has the same db struct tag as the id field on the Person struct? </p></pre>adelowo: <pre><p>Yes, the id would be correctly set on the <code>Location</code> struct.. I don&#39;t know of anyother way around ths.</p></pre>: <pre><p>[deleted]</p></pre>adelowo: <pre><p>Thanks for the correction.</p></pre>adelowo: <pre><p>Thanks for the correction.</p></pre>

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

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