Xorm, default values of columns and BeforeInsert

agolangf · · 2446 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey <a href="/r/golang" rel="nofollow">/r/golang</a> !</p> <p>So I am trying/learning xorm ( <a href="http://xorm.io/" rel="nofollow">http://xorm.io/</a> )</p> <p>In one of my tables I have a column which value is random, in the sense that the default value is the result of the sql function RANDOM() (I&#39;m using PostgreSQL, but this should apply to other rdbmses too).</p> <p>I have a struct modeled accordingly:</p> <pre><code>type MyType struct { ... random_value float64 `xorm:&#34;DOUBLE NOT NULL DEFAULT random()&#34;` } </code></pre> <p>I realized that upon instantiating a struct of type MyStruct, since go defaults numeric types to zero (and other types to the relative null/zero/void value), the random_value field of every struct will get the value 0, which is a valid float64 value.</p> <p>Thus, the rdbms is never going to execute the random() function in order to set the value of that column.</p> <p><strong>First question</strong>: is there a way to instruct xorm to omit a field when constructing the INSERT sql query ?</p> <p>I tried working around this behaviour: In the &#34;Events&#34; section of the documentation ( <a href="http://gobook.io/read/go-xorm/manual-en-us/chapter-12/" rel="nofollow">http://gobook.io/read/go-xorm/manual-en-us/chapter-12/</a> ) is explained how to intercept and handle events such as insertion, update, deletion and so on.</p> <p>It is done by defining, for example, a BeforeInsert method on the struct, like this:</p> <pre><code>func (self MyType) BeforeInsert() { ... } </code></pre> <p>I tried using this technique to set the value of the random_value field. The problem is that this way of defining the method is incapable of modifying the value of fields in the struct since the struct is passed by value.</p> <p>I suspect the correct way to implement this would have been passing the struct by pointer (which is also more memory-efficient), like this:</p> <pre><code>func (self *MyType) BeforeInsert() { ... } </code></pre> <p>I tried implementing this, but such variation does not get called at all (I verified this by doing some Println, the old and dumb way).</p> <p><strong>Second question</strong>: How do I customize a struct before insertion? Apparently you can have read access to all members of your struct but no write-access.</p> <p>I have the feel that xorm is a very nice library which works very well until your use case falls in the ones envisioned by its creator, but fails very badly when you&#39;re trying to do something <em>particular</em>, outside the usual web-related CRUD. Also, the documentation is very basic and in some circumstances not so nice (the web pages at <a href="http://gobook.io/read/go-xorm/manual-en-us/" rel="nofollow">http://gobook.io/read/go-xorm/manual-en-us/</a> render a div incorrectly overlapping, and I had to supply my own css fix via stylish firefox extension to have something readable). Also, the source code lacks documentation, resulting in a godoc very little useful.</p> <p>I would greatly appreciate hints and suggestions regarding my problem, as well as suggestions to other libraries that do not have such problems.</p> <h2>Thanks in advance,</h2> <p>znpy</p>

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

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