How to create go structs for existing MySQL tables?

xuanbao · · 708 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi I was never dealing with database programming +go so am after some advice. I have a database with few tables, some of them are related using foreign keys. Not smth complex, but I thought there has to be some auto solution that shall let me create corresponding go structs to use with let&#39;s say dbr package. But for some reason I spent like a day looking for it and really didn&#39;t find anything. The only thing close to what I was looking for seemed knq/xo package but it shows error</p> <blockquote> <p>error: Error 1109: Unknown table &#39;parameters&#39; in information_schema</p> </blockquote> <p>When I try using it.</p> <p>I thought this task is common but somehow I stucked with it.</p> <hr/>**评论:**<br/><br/>Tikiatua: <pre><p>Take a look at sqlboiler <a href="https://github.com/vattle/sqlboiler" rel="nofollow">https://github.com/vattle/sqlboiler</a></p></pre>twek: <pre><p>This one, love it, I only wish it worked with Microsoft SQL as I&#39;m forced to use that at work</p></pre>sergeykurt: <pre><p>Take a look on this PR - <a href="https://github.com/vattle/sqlboiler/pull/119" rel="nofollow">MS SQL engine support #119</a></p></pre>twek: <pre><p>Thanks for this. I will be watching :))</p></pre>chmikes: <pre><p>What you describe is called an ORM. It maps programming language specific data structures with database tables. Xorm looks nice. Here is its documentation. <a href="http://xorm.io/docs/" rel="nofollow">http://xorm.io/docs/</a>. There are many ORM with many different functionalities and properties. Xorm has nice functionalities. One interesting property of ORM is that it can be used with many different databases. So you can develop and test your code with SQLight and use MySQL or PostgreSQL in production. </p> <p>I didn&#39;t use it yet, but I plan to use it once I start developping the database backend of my software.</p></pre>lunnyxiao: <pre><p>maybe you can try github.com/go-xorm/cmd</p></pre>jimmeyotoole: <pre><p>Whilst there are orm for go as people are suggesting, I recommend sqlx which just had really nice helpers for saving and getting from a usual dB. It hooks right into regular SQL go package too</p></pre>fraenky8: <pre><p>Maybe this is useful for you: <a href="https://github.com/fraenky8/tables-to-go" rel="nofollow">https://github.com/fraenky8/tables-to-go</a></p></pre>nkumar15: <pre><p>I think you could give a try to <a href="https://upper.io/db.v3/" rel="nofollow">https://upper.io/db.v3/</a></p> <p>Its a nice library to write Data Access Layer logic without writing much of Sql queries explicitly.</p></pre>knq: <pre><p><code>xo</code> should work fine with most MySQL databases. Are you using a old MySQL version? If you could provide more information about the database version you&#39;re using, I can help find out what the issue is.</p></pre>

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

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