Rebuild documents

This commit is contained in:
Jinzhu 2016-02-29 21:52:18 +08:00
parent 49efcd81c7
commit 1fbfb00bae
12 changed files with 1259 additions and 692 deletions

View File

@ -17,7 +17,7 @@ The database handle returned from db.Begin() should be used for all oper">
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css"> <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
@ -71,7 +71,7 @@ The database handle returned from db.Begin() should be used for all oper">
data-chapter-title="Advanced Usage" data-chapter-title="Advanced Usage"
data-filepath="advanced.md" data-filepath="advanced.md"
data-basepath="" data-basepath=""
data-revision="Mon Feb 29 2016 21:35:06 GMT+0800 (CST)" data-revision="Mon Feb 29 2016 21:51:55 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -602,119 +602,119 @@ The database handle returned from db.Begin() should be used for all oper">
</ul> </ul>
<!-- toc stop --> <!-- toc stop -->
<h2 id="error-handling">Error Handling</h2> <h2 id="error-handling">Error Handling</h2>
<pre><code class="lang-go">query <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> <pre><code class="lang-go">query := db.Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).First(&amp;user)
query <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Limit<span class="token punctuation">(</span></span><span class="token number">10</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> query := db.First(&amp;user).Limit(<span class="hljs-number">10</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">// query.Error will return the last happened error</span> <span class="hljs-comment">// query.Error will return the last happened error</span>
<span class="token comment" spellcheck="true">// So you could do error handing in your application like this:</span> <span class="hljs-comment">// So you could do error handing in your application like this:</span>
<span class="token keyword">if</span> err <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span>Error<span class="token operator">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token operator">{</span> <span class="hljs-keyword">if</span> err := db.Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).First(&amp;user).Error; err != <span class="hljs-constant">nil</span> {
<span class="token comment" spellcheck="true">// error handling...</span> <span class="hljs-comment">// error handling...</span>
<span class="token operator">}</span> }
<span class="token comment" spellcheck="true">// RecordNotFound</span> <span class="hljs-comment">// RecordNotFound</span>
<span class="token comment" spellcheck="true">// If no record found when you query data, gorm will return RecordNotFound error, you could check it like this:</span> <span class="hljs-comment">// If no record found when you query data, gorm will return RecordNotFound error, you could check it like this:</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;hello world&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span>Error <span class="token operator">==</span> gorm<span class="token operator">.</span>RecordNotFound db.Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;hello world&quot;</span>).First(&amp;User{}).Error == gorm.RecordNotFound
<span class="token comment" spellcheck="true">// Or use the shortcut method</span> <span class="hljs-comment">// Or use the shortcut method</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;hello world&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">RecordNotFound<span class="token punctuation">(</span></span><span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;hello world&quot;</span>).First(&amp;user).RecordNotFound()
<span class="token keyword">if</span> db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Related<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>credit_card<span class="token operator">)</span><span class="token operator">.</span><span class="token function">RecordNotFound<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">if</span> db.Model(&amp;user).Related(&amp;credit_card).RecordNotFound() {
<span class="token comment" spellcheck="true">// no credit card found error handling</span> <span class="hljs-comment">// no credit card found error handling</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h2 id="transactions">Transactions</h2> <h2 id="transactions">Transactions</h2>
<p>To perform a set of operations within a transaction, the general flow is as below. <p>To perform a set of operations within a transaction, the general flow is as below.
The database handle returned from <code>db.Begin()</code> should be used for all operations within the transaction. The database handle returned from <code>db.Begin()</code> should be used for all operations within the transaction.
(Note that all individual save and delete operations are run in a transaction by default.)</p> (Note that all individual save and delete operations are run in a transaction by default.)</p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// begin</span> <pre><code class="lang-go"><span class="hljs-comment">// begin</span>
tx <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">Begin<span class="token punctuation">(</span></span><span class="token operator">)</span> tx := db.Begin()
<span class="token comment" spellcheck="true">// do some database operations (use &apos;tx&apos; from this point, not &apos;db&apos;)</span> <span class="hljs-comment">// do some database operations (use &apos;tx&apos; from this point, not &apos;db&apos;)</span>
tx<span class="token operator">.</span><span class="token function">Create<span class="token punctuation">(</span></span><span class="token operator">...</span><span class="token operator">)</span> tx.Create(...)
<span class="token operator">...</span> ...
<span class="token comment" spellcheck="true">// rollback in case of error</span> <span class="hljs-comment">// rollback in case of error</span>
tx<span class="token operator">.</span><span class="token function">Rollback<span class="token punctuation">(</span></span><span class="token operator">)</span> tx.Rollback()
<span class="token comment" spellcheck="true">// Or commit if all is ok</span> <span class="hljs-comment">// Or commit if all is ok</span>
tx<span class="token operator">.</span><span class="token function">Commit<span class="token punctuation">(</span></span><span class="token operator">)</span> tx.Commit()
</code></pre> </code></pre>
<h3 id="a-specific-example">A Specific Example</h3> <h3 id="a-specific-example">A Specific Example</h3>
<pre><code class="lang-go"><span class="token keyword">func</span> <span class="token function">CreateAnimals<span class="token punctuation">(</span></span>db <span class="token operator">*</span>gorm<span class="token operator">.</span>DB<span class="token operator">)</span> err <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">func</span> CreateAnimals(db *gorm.DB) err {
tx <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">Begin<span class="token punctuation">(</span></span><span class="token operator">)</span> tx := db.Begin()
<span class="token comment" spellcheck="true">// Note the use of tx as the database handle once you are within a transaction</span> <span class="hljs-comment">// Note the use of tx as the database handle once you are within a transaction</span>
<span class="token keyword">if</span> err <span class="token operator">:=</span> tx<span class="token operator">.</span><span class="token function">Create<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>Animal<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;Giraffe&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span>Error<span class="token operator">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token operator">{</span> <span class="hljs-keyword">if</span> err := tx.Create(&amp;Animal{Name: <span class="hljs-string">&quot;Giraffe&quot;</span>}).Error; err != <span class="hljs-constant">nil</span> {
tx<span class="token operator">.</span><span class="token function">Rollback<span class="token punctuation">(</span></span><span class="token operator">)</span> tx.Rollback()
<span class="token keyword">return</span> err <span class="hljs-keyword">return</span> err
<span class="token operator">}</span> }
<span class="token keyword">if</span> err <span class="token operator">:=</span> tx<span class="token operator">.</span><span class="token function">Create<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>Animal<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;Lion&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span>Error<span class="token operator">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token operator">{</span> <span class="hljs-keyword">if</span> err := tx.Create(&amp;Animal{Name: <span class="hljs-string">&quot;Lion&quot;</span>}).Error; err != <span class="hljs-constant">nil</span> {
tx<span class="token operator">.</span><span class="token function">Rollback<span class="token punctuation">(</span></span><span class="token operator">)</span> tx.Rollback()
<span class="token keyword">return</span> err <span class="hljs-keyword">return</span> err
<span class="token operator">}</span> }
tx<span class="token operator">.</span><span class="token function">Commit<span class="token punctuation">(</span></span><span class="token operator">)</span> tx.Commit()
<span class="token keyword">return</span> <span class="token boolean">nil</span> <span class="hljs-keyword">return</span> <span class="hljs-constant">nil</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h2 id="raw-sql">Raw SQL</h2> <h2 id="raw-sql">Raw SQL</h2>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Exec<span class="token punctuation">(</span></span><span class="token string">&quot;DROP TABLE users;&quot;</span><span class="token operator">)</span> <pre><code class="lang-go">db.Exec(<span class="hljs-string">&quot;DROP TABLE users;&quot;</span>)
db<span class="token operator">.</span><span class="token function">Exec<span class="token punctuation">(</span></span><span class="token string">&quot;UPDATE orders SET shipped_at=? WHERE id IN (?)&quot;</span><span class="token operator">,</span> time<span class="token operator">.</span>Now<span class="token operator">,</span> <span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">int64</span><span class="token operator">{</span><span class="token number">11</span><span class="token operator">,</span><span class="token number">22</span><span class="token operator">,</span><span class="token number">33</span><span class="token operator">}</span><span class="token operator">)</span> db.Exec(<span class="hljs-string">&quot;UPDATE orders SET shipped_at=? WHERE id IN (?)&quot;</span>, time.Now, []<span class="hljs-typename">int64</span>{<span class="hljs-number">11</span>,<span class="hljs-number">22</span>,<span class="hljs-number">33</span>})
</code></pre> </code></pre>
<h2 id="row--rows">Row &amp; Rows</h2> <h2 id="row--rows">Row &amp; Rows</h2>
<p>It is even possible to get query result as <code>*sql.Row</code> or <code>*sql.Rows</code></p> <p>It is even possible to get query result as <code>*sql.Row</code> or <code>*sql.Rows</code></p>
<pre><code class="lang-go">row <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;users&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;name, age&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Row<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token comment" spellcheck="true">// (*sql.Row)</span> <pre><code class="lang-go">row := db.Table(<span class="hljs-string">&quot;users&quot;</span>).Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Select(<span class="hljs-string">&quot;name, age&quot;</span>).Row() <span class="hljs-comment">// (*sql.Row)</span>
row<span class="token operator">.</span><span class="token function">Scan<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>name<span class="token operator">,</span> <span class="token operator">&amp;</span>age<span class="token operator">)</span> row.Scan(&amp;name, &amp;age)
rows<span class="token operator">,</span> err <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;name, age, email&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Rows<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token comment" spellcheck="true">// (*sql.Rows, error)</span> rows, err := db.Model(&amp;User{}).Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Select(<span class="hljs-string">&quot;name, age, email&quot;</span>).Rows() <span class="hljs-comment">// (*sql.Rows, error)</span>
<span class="token keyword">defer</span> rows<span class="token operator">.</span><span class="token function">Close<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="hljs-keyword">defer</span> rows.Close()
<span class="token keyword">for</span> rows<span class="token operator">.</span><span class="token function">Next<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">for</span> rows.Next() {
<span class="token operator">...</span> ...
rows<span class="token operator">.</span><span class="token function">Scan<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>name<span class="token operator">,</span> <span class="token operator">&amp;</span>age<span class="token operator">,</span> <span class="token operator">&amp;</span>email<span class="token operator">)</span> rows.Scan(&amp;name, &amp;age, &amp;email)
<span class="token operator">...</span> ...
<span class="token operator">}</span> }
<span class="token comment" spellcheck="true">// Raw SQL</span> <span class="hljs-comment">// Raw SQL</span>
rows<span class="token operator">,</span> err <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">Raw<span class="token punctuation">(</span></span><span class="token string">&quot;select name, age, email from users where name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Rows<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token comment" spellcheck="true">// (*sql.Rows, error)</span> rows, err := db.Raw(<span class="hljs-string">&quot;select name, age, email from users where name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Rows() <span class="hljs-comment">// (*sql.Rows, error)</span>
<span class="token keyword">defer</span> rows<span class="token operator">.</span><span class="token function">Close<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="hljs-keyword">defer</span> rows.Close()
<span class="token keyword">for</span> rows<span class="token operator">.</span><span class="token function">Next<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">for</span> rows.Next() {
<span class="token operator">...</span> ...
rows<span class="token operator">.</span><span class="token function">Scan<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>name<span class="token operator">,</span> <span class="token operator">&amp;</span>age<span class="token operator">,</span> <span class="token operator">&amp;</span>email<span class="token operator">)</span> rows.Scan(&amp;name, &amp;age, &amp;email)
<span class="token operator">...</span> ...
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h3 id="scan-rows">Scan Rows</h3> <h3 id="scan-rows">Scan Rows</h3>
<pre><code class="lang-go">rows<span class="token operator">,</span> err <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;name, age, email&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Rows<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token comment" spellcheck="true">// (*sql.Rows, error)</span> <pre><code class="lang-go">rows, err := db.Model(&amp;User{}).Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Select(<span class="hljs-string">&quot;name, age, email&quot;</span>).Rows() <span class="hljs-comment">// (*sql.Rows, error)</span>
<span class="token keyword">defer</span> rows<span class="token operator">.</span><span class="token function">Close<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="hljs-keyword">defer</span> rows.Close()
<span class="token keyword">for</span> rows<span class="token operator">.</span><span class="token function">Next<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">for</span> rows.Next() {
<span class="token keyword">var</span> user User <span class="hljs-keyword">var</span> user User
db<span class="token operator">.</span><span class="token function">ScanRows<span class="token punctuation">(</span></span>rows<span class="token operator">,</span> <span class="token operator">&amp;</span>user<span class="token operator">)</span> db.ScanRows(rows, &amp;user)
<span class="token comment" spellcheck="true">// do something</span> <span class="hljs-comment">// do something</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h2 id="composite-primary-key">Composite Primary Key</h2> <h2 id="composite-primary-key">Composite Primary Key</h2>
<pre><code class="lang-go"><span class="token keyword">type</span> Product <span class="token keyword">struct</span> <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">type</span> Product <span class="hljs-keyword">struct</span> {
ID <span class="token builtin">string</span> <span class="token string">`gorm:&quot;primary_key&quot;`</span> ID <span class="hljs-typename">string</span> <span class="hljs-string">`gorm:&quot;primary_key&quot;`</span>
LanguageCode <span class="token builtin">string</span> <span class="token string">`gorm:&quot;primary_key&quot;`</span> LanguageCode <span class="hljs-typename">string</span> <span class="hljs-string">`gorm:&quot;primary_key&quot;`</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h2 id="logger">Logger</h2> <h2 id="logger">Logger</h2>
<p>Gorm has built-in logger support</p> <p>Gorm has built-in logger support</p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Enable Logger</span> <pre><code class="lang-go"><span class="hljs-comment">// Enable Logger</span>
db<span class="token operator">.</span><span class="token function">LogMode<span class="token punctuation">(</span></span><span class="token boolean">true</span><span class="token operator">)</span> db.LogMode(<span class="hljs-constant">true</span>)
<span class="token comment" spellcheck="true">// Diable Logger</span> <span class="hljs-comment">// Diable Logger</span>
db<span class="token operator">.</span><span class="token function">LogMode<span class="token punctuation">(</span></span><span class="token boolean">false</span><span class="token operator">)</span> db.LogMode(<span class="hljs-constant">false</span>)
<span class="token comment" spellcheck="true">// Debug a single operation</span> <span class="hljs-comment">// Debug a single operation</span>
db<span class="token operator">.</span><span class="token function">Debug<span class="token punctuation">(</span></span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span> db.Debug().Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).First(&amp;User{})
</code></pre> </code></pre>
<p><img src="https:/raw.github.com/jinzhu/gorm/master/doc/logger.png" alt="logger"></p> <p><img src="https:/raw.github.com/jinzhu/gorm/master/doc/logger.png" alt="logger"></p>
<h3 id="customize-logger">Customize Logger</h3> <h3 id="customize-logger">Customize Logger</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Refer gorm&apos;s default logger for how to: https://github.com/jinzhu/gorm/blob/master/logger.go#files</span> <pre><code class="lang-go"><span class="hljs-comment">// Refer gorm&apos;s default logger for how to: https://github.com/jinzhu/gorm/blob/master/logger.go#files</span>
db<span class="token operator">.</span><span class="token function">SetLogger<span class="token punctuation">(</span></span>gorm<span class="token operator">.</span>Logger<span class="token operator">{</span>revel<span class="token operator">.</span>TRACE<span class="token operator">}</span><span class="token operator">)</span> db.SetLogger(gorm.Logger{revel.TRACE})
db<span class="token operator">.</span><span class="token function">SetLogger<span class="token punctuation">(</span></span>log<span class="token operator">.</span><span class="token function">New<span class="token punctuation">(</span></span>os<span class="token operator">.</span>Stdout<span class="token operator">,</span> <span class="token string">&quot;\r\n&quot;</span><span class="token operator">,</span> <span class="token number">0</span><span class="token operator">)</span><span class="token operator">)</span> db.SetLogger(log.New(os.Stdout, <span class="hljs-string">&quot;\r\n&quot;</span>, <span class="hljs-number">0</span>))
</code></pre> </code></pre>
@ -773,7 +773,7 @@ db<span class="token operator">.</span><span class="token function">SetLogger<sp
<script> <script>
require(["gitbook"], function(gitbook) { require(["gitbook"], function(gitbook) {
gitbook.start({"fontsettings":{"theme":"night","family":"sans","size":4},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"prism":{},"anker-enable":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}}); gitbook.start({"fontsettings":{"theme":"night","size":2,"family":"sans"},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"anker-enable":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}});
}); });
</script> </script>

View File

@ -16,7 +16,7 @@
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css"> <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
@ -70,7 +70,7 @@
data-chapter-title="Associations" data-chapter-title="Associations"
data-filepath="associations.md" data-filepath="associations.md"
data-basepath="" data-basepath=""
data-revision="Mon Feb 29 2016 21:35:06 GMT+0800 (CST)" data-revision="Mon Feb 29 2016 21:51:55 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -592,130 +592,130 @@
</ul> </ul>
<!-- toc stop --> <!-- toc stop -->
<h2 id="belongs-to">Belongs To</h2> <h2 id="belongs-to">Belongs To</h2>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// User belongs to a profile, ProfileID is the foreign key</span> <pre><code class="lang-go"><span class="hljs-comment">// User belongs to a profile, ProfileID is the foreign key</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> User <span class="hljs-keyword">struct</span> {
gorm<span class="token operator">.</span>Model gorm.Model
Profile Profile Profile Profile
ProfileID <span class="token builtin">int</span> ProfileID <span class="hljs-typename">int</span>
<span class="token operator">}</span> }
<span class="token keyword">type</span> Profile <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> Profile <span class="hljs-keyword">struct</span> {
gorm<span class="token operator">.</span>Model gorm.Model
Name <span class="token builtin">string</span> Name <span class="hljs-typename">string</span>
<span class="token operator">}</span> }
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Related<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>profile<span class="token operator">)</span> db.Model(&amp;user).Related(&amp;profile)
<span class="token comment" spellcheck="true">//// SELECT * FROM profiles WHERE id = 111; // 111 is user&apos;s foreign key ProfileID</span> <span class="hljs-comment">//// SELECT * FROM profiles WHERE id = 111; // 111 is user&apos;s foreign key ProfileID</span>
</code></pre> </code></pre>
<h2 id="has-one">Has One</h2> <h2 id="has-one">Has One</h2>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// User has one CreditCard, UserID is the foreign key</span> <pre><code class="lang-go"><span class="hljs-comment">// User has one CreditCard, UserID is the foreign key</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> User <span class="hljs-keyword">struct</span> {
gorm<span class="token operator">.</span>Model gorm.Model
CreditCard CreditCard CreditCard CreditCard
<span class="token operator">}</span> }
<span class="token keyword">type</span> CreditCard <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> CreditCard <span class="hljs-keyword">struct</span> {
gorm<span class="token operator">.</span>Model gorm.Model
UserID <span class="token builtin">uint</span> UserID <span class="hljs-typename">uint</span>
Number <span class="token builtin">string</span> Number <span class="hljs-typename">string</span>
<span class="token operator">}</span> }
<span class="token keyword">var</span> card CreditCard <span class="hljs-keyword">var</span> card CreditCard
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Related<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>card<span class="token operator">,</span> <span class="token string">&quot;CreditCard&quot;</span><span class="token operator">)</span> db.Model(&amp;user).Related(&amp;card, <span class="hljs-string">&quot;CreditCard&quot;</span>)
<span class="token comment" spellcheck="true">//// SELECT * FROM credit_cards WHERE user_id = 123; // 123 is user&apos;s primary key</span> <span class="hljs-comment">//// SELECT * FROM credit_cards WHERE user_id = 123; // 123 is user&apos;s primary key</span>
<span class="token comment" spellcheck="true">// CreditCard is user&apos;s field name, it means get user&apos;s CreditCard relations and fill it into variable card</span> <span class="hljs-comment">// CreditCard is user&apos;s field name, it means get user&apos;s CreditCard relations and fill it into variable card</span>
<span class="token comment" spellcheck="true">// If the field name is same as the variable&apos;s type name, like above example, it could be omitted, like:</span> <span class="hljs-comment">// If the field name is same as the variable&apos;s type name, like above example, it could be omitted, like:</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Related<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>card<span class="token operator">)</span> db.Model(&amp;user).Related(&amp;card)
</code></pre> </code></pre>
<h2 id="has-many">Has Many</h2> <h2 id="has-many">Has Many</h2>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// User has many emails, UserID is the foreign key</span> <pre><code class="lang-go"><span class="hljs-comment">// User has many emails, UserID is the foreign key</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> User <span class="hljs-keyword">struct</span> {
gorm<span class="token operator">.</span>Model gorm.Model
Emails <span class="token operator">[</span><span class="token operator">]</span>Email Emails []Email
<span class="token operator">}</span> }
<span class="token keyword">type</span> Email <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> Email <span class="hljs-keyword">struct</span> {
gorm<span class="token operator">.</span>Model gorm.Model
Email <span class="token builtin">string</span> Email <span class="hljs-typename">string</span>
UserID <span class="token builtin">uint</span> UserID <span class="hljs-typename">uint</span>
<span class="token operator">}</span> }
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Related<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>emails<span class="token operator">)</span> db.Model(&amp;user).Related(&amp;emails)
<span class="token comment" spellcheck="true">//// SELECT * FROM emails WHERE user_id = 111; // 111 is user&apos;s primary key</span> <span class="hljs-comment">//// SELECT * FROM emails WHERE user_id = 111; // 111 is user&apos;s primary key</span>
</code></pre> </code></pre>
<h2 id="many-to-many">Many To Many</h2> <h2 id="many-to-many">Many To Many</h2>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// User has and belongs to many languages, use `user_languages` as join table</span> <pre><code class="lang-go"><span class="hljs-comment">// User has and belongs to many languages, use `user_languages` as join table</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> User <span class="hljs-keyword">struct</span> {
gorm<span class="token operator">.</span>Model gorm.Model
Languages <span class="token operator">[</span><span class="token operator">]</span>Language <span class="token string">`gorm:&quot;many2many:user_languages;&quot;`</span> Languages []Language <span class="hljs-string">`gorm:&quot;many2many:user_languages;&quot;`</span>
<span class="token operator">}</span> }
<span class="token keyword">type</span> Language <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> Language <span class="hljs-keyword">struct</span> {
gorm<span class="token operator">.</span>Model gorm.Model
Name <span class="token builtin">string</span> Name <span class="hljs-typename">string</span>
<span class="token operator">}</span> }
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Related<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>languages<span class="token operator">)</span> db.Model(&amp;user).Related(&amp;languages)
<span class="token comment" spellcheck="true">//// SELECT * FROM &quot;languages&quot; INNER JOIN &quot;user_languages&quot; ON &quot;user_languages&quot;.&quot;language_id&quot; = &quot;languages&quot;.&quot;id&quot; WHERE &quot;user_languages&quot;.&quot;user_id&quot; = 111</span> <span class="hljs-comment">//// SELECT * FROM &quot;languages&quot; INNER JOIN &quot;user_languages&quot; ON &quot;user_languages&quot;.&quot;language_id&quot; = &quot;languages&quot;.&quot;id&quot; WHERE &quot;user_languages&quot;.&quot;user_id&quot; = 111</span>
</code></pre> </code></pre>
<h2 id="polymorphism">Polymorphism</h2> <h2 id="polymorphism">Polymorphism</h2>
<p>Supports polymorphic has-many and has-one associations.</p> <p>Supports polymorphic has-many and has-one associations.</p>
<pre><code class="lang-go"> <span class="token keyword">type</span> Cat <span class="token keyword">struct</span> <span class="token operator">{</span> <pre><code class="lang-go"> <span class="hljs-keyword">type</span> Cat <span class="hljs-keyword">struct</span> {
Id <span class="token builtin">int</span> Id <span class="hljs-typename">int</span>
Name <span class="token builtin">string</span> Name <span class="hljs-typename">string</span>
Toy Toy <span class="token string">`gorm:&quot;polymorphic:Owner;&quot;`</span> Toy Toy <span class="hljs-string">`gorm:&quot;polymorphic:Owner;&quot;`</span>
<span class="token operator">}</span> }
<span class="token keyword">type</span> Dog <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> Dog <span class="hljs-keyword">struct</span> {
Id <span class="token builtin">int</span> Id <span class="hljs-typename">int</span>
Name <span class="token builtin">string</span> Name <span class="hljs-typename">string</span>
Toy Toy <span class="token string">`gorm:&quot;polymorphic:Owner;&quot;`</span> Toy Toy <span class="hljs-string">`gorm:&quot;polymorphic:Owner;&quot;`</span>
<span class="token operator">}</span> }
<span class="token keyword">type</span> Toy <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> Toy <span class="hljs-keyword">struct</span> {
Id <span class="token builtin">int</span> Id <span class="hljs-typename">int</span>
Name <span class="token builtin">string</span> Name <span class="hljs-typename">string</span>
OwnerId <span class="token builtin">int</span> OwnerId <span class="hljs-typename">int</span>
OwnerType <span class="token builtin">string</span> OwnerType <span class="hljs-typename">string</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
<p>Note: polymorphic belongs-to and many-to-many are explicitly NOT supported, and will throw errors.</p> <p>Note: polymorphic belongs-to and many-to-many are explicitly NOT supported, and will throw errors.</p>
<h2 id="association-mode">Association Mode</h2> <h2 id="association-mode">Association Mode</h2>
<p>Association Mode contains some helper methods to handle relationship things easily.</p> <p>Association Mode contains some helper methods to handle relationship things easily.</p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Start Association Mode</span> <pre><code class="lang-go"><span class="hljs-comment">// Start Association Mode</span>
<span class="token keyword">var</span> user User <span class="hljs-keyword">var</span> user User
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Association<span class="token punctuation">(</span></span><span class="token string">&quot;Languages&quot;</span><span class="token operator">)</span> db.Model(&amp;user).Association(<span class="hljs-string">&quot;Languages&quot;</span>)
<span class="token comment" spellcheck="true">// `user` is the source, it need to be a valid record (contains primary key)</span> <span class="hljs-comment">// `user` is the source, it need to be a valid record (contains primary key)</span>
<span class="token comment" spellcheck="true">// `Languages` is source&apos;s field name for a relationship.</span> <span class="hljs-comment">// `Languages` is source&apos;s field name for a relationship.</span>
<span class="token comment" spellcheck="true">// If those conditions not matched, will return an error, check it with:</span> <span class="hljs-comment">// If those conditions not matched, will return an error, check it with:</span>
<span class="token comment" spellcheck="true">// db.Model(&amp;user).Association(&quot;Languages&quot;).Error</span> <span class="hljs-comment">// db.Model(&amp;user).Association(&quot;Languages&quot;).Error</span>
<span class="token comment" spellcheck="true">// Query - Find out all related associations</span> <span class="hljs-comment">// Query - Find out all related associations</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Association<span class="token punctuation">(</span></span><span class="token string">&quot;Languages&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>languages<span class="token operator">)</span> db.Model(&amp;user).Association(<span class="hljs-string">&quot;Languages&quot;</span>).Find(&amp;languages)
<span class="token comment" spellcheck="true">// Append - Append new associations for many2many, has_many, will replace current association for has_one, belongs_to</span> <span class="hljs-comment">// Append - Append new associations for many2many, has_many, will replace current association for has_one, belongs_to</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Association<span class="token punctuation">(</span></span><span class="token string">&quot;Languages&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Append<span class="token punctuation">(</span></span><span class="token operator">[</span><span class="token operator">]</span>Language<span class="token operator">{</span>languageZH<span class="token operator">,</span> languageEN<span class="token operator">}</span><span class="token operator">)</span> db.Model(&amp;user).Association(<span class="hljs-string">&quot;Languages&quot;</span>).Append([]Language{languageZH, languageEN})
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Association<span class="token punctuation">(</span></span><span class="token string">&quot;Languages&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Append<span class="token punctuation">(</span></span>Language<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;DE&quot;</span><span class="token operator">}</span><span class="token operator">)</span> db.Model(&amp;user).Association(<span class="hljs-string">&quot;Languages&quot;</span>).Append(Language{Name: <span class="hljs-string">&quot;DE&quot;</span>})
<span class="token comment" spellcheck="true">// Delete - Remove relationship between source &amp; passed arguments, won&apos;t delete those arguments</span> <span class="hljs-comment">// Delete - Remove relationship between source &amp; passed arguments, won&apos;t delete those arguments</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Association<span class="token punctuation">(</span></span><span class="token string">&quot;Languages&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Delete<span class="token punctuation">(</span></span><span class="token operator">[</span><span class="token operator">]</span>Language<span class="token operator">{</span>languageZH<span class="token operator">,</span> languageEN<span class="token operator">}</span><span class="token operator">)</span> db.Model(&amp;user).Association(<span class="hljs-string">&quot;Languages&quot;</span>).Delete([]Language{languageZH, languageEN})
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Association<span class="token punctuation">(</span></span><span class="token string">&quot;Languages&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Delete<span class="token punctuation">(</span></span>languageZH<span class="token operator">,</span> languageEN<span class="token operator">)</span> db.Model(&amp;user).Association(<span class="hljs-string">&quot;Languages&quot;</span>).Delete(languageZH, languageEN)
<span class="token comment" spellcheck="true">// Replace - Replace current associations with new one</span> <span class="hljs-comment">// Replace - Replace current associations with new one</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Association<span class="token punctuation">(</span></span><span class="token string">&quot;Languages&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Replace<span class="token punctuation">(</span></span><span class="token operator">[</span><span class="token operator">]</span>Language<span class="token operator">{</span>languageZH<span class="token operator">,</span> languageEN<span class="token operator">}</span><span class="token operator">)</span> db.Model(&amp;user).Association(<span class="hljs-string">&quot;Languages&quot;</span>).Replace([]Language{languageZH, languageEN})
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Association<span class="token punctuation">(</span></span><span class="token string">&quot;Languages&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Replace<span class="token punctuation">(</span></span>Language<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;DE&quot;</span><span class="token operator">}</span><span class="token operator">,</span> languageEN<span class="token operator">)</span> db.Model(&amp;user).Association(<span class="hljs-string">&quot;Languages&quot;</span>).Replace(Language{Name: <span class="hljs-string">&quot;DE&quot;</span>}, languageEN)
<span class="token comment" spellcheck="true">// Count - Return the count of current associations</span> <span class="hljs-comment">// Count - Return the count of current associations</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Association<span class="token punctuation">(</span></span><span class="token string">&quot;Languages&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Count<span class="token punctuation">(</span></span><span class="token operator">)</span> db.Model(&amp;user).Association(<span class="hljs-string">&quot;Languages&quot;</span>).Count()
<span class="token comment" spellcheck="true">// Clear - Remove relationship between source &amp; current associations, won&apos;t delete those associations</span> <span class="hljs-comment">// Clear - Remove relationship between source &amp; current associations, won&apos;t delete those associations</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Association<span class="token punctuation">(</span></span><span class="token string">&quot;Languages&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Clear<span class="token punctuation">(</span></span><span class="token operator">)</span> db.Model(&amp;user).Association(<span class="hljs-string">&quot;Languages&quot;</span>).Clear()
</code></pre> </code></pre>
@ -774,7 +774,7 @@ db<span class="token operator">.</span><span class="token function">Model<span c
<script> <script>
require(["gitbook"], function(gitbook) { require(["gitbook"], function(gitbook) {
gitbook.start({"fontsettings":{"theme":"night","family":"sans","size":4},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"prism":{},"anker-enable":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}}); gitbook.start({"fontsettings":{"theme":"night","size":2,"family":"sans"},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"anker-enable":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}});
}); });
</script> </script>

View File

@ -17,7 +17,7 @@ If any callback returns an error, gorm will stop future operations and rollback
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css"> <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
@ -71,7 +71,7 @@ If any callback returns an error, gorm will stop future operations and rollback
data-chapter-title="Callbacks" data-chapter-title="Callbacks"
data-filepath="callbacks.md" data-filepath="callbacks.md"
data-basepath="" data-basepath=""
data-revision="Mon Feb 29 2016 21:35:06 GMT+0800 (CST)" data-revision="Mon Feb 29 2016 21:51:55 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -598,54 +598,54 @@ If any callback returns an error, gorm will stop future operations and rollback
<h3 id="creating-an-object">Creating An Object</h3> <h3 id="creating-an-object">Creating An Object</h3>
<pre><code class="lang-go">BeforeSave <pre><code class="lang-go">BeforeSave
BeforeCreate BeforeCreate
<span class="token comment" spellcheck="true">// save before associations</span> <span class="hljs-comment">// save before associations</span>
<span class="token comment" spellcheck="true">// save self</span> <span class="hljs-comment">// save self</span>
<span class="token comment" spellcheck="true">// save after associations</span> <span class="hljs-comment">// save after associations</span>
AfterCreate AfterCreate
AfterSave AfterSave
</code></pre> </code></pre>
<h3 id="updating-an-object">Updating An Object</h3> <h3 id="updating-an-object">Updating An Object</h3>
<pre><code class="lang-go">BeforeSave <pre><code class="lang-go">BeforeSave
BeforeUpdate BeforeUpdate
<span class="token comment" spellcheck="true">// save before associations</span> <span class="hljs-comment">// save before associations</span>
<span class="token comment" spellcheck="true">// save self</span> <span class="hljs-comment">// save self</span>
<span class="token comment" spellcheck="true">// save after associations</span> <span class="hljs-comment">// save after associations</span>
AfterUpdate AfterUpdate
AfterSave AfterSave
</code></pre> </code></pre>
<h3 id="destroying-an-object">Destroying An Object</h3> <h3 id="destroying-an-object">Destroying An Object</h3>
<pre><code class="lang-go">BeforeDelete <pre><code class="lang-go">BeforeDelete
<span class="token comment" spellcheck="true">// delete self</span> <span class="hljs-comment">// delete self</span>
AfterDelete AfterDelete
</code></pre> </code></pre>
<h3 id="after-find">After Find</h3> <h3 id="after-find">After Find</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// load data from database</span> <pre><code class="lang-go"><span class="hljs-comment">// load data from database</span>
AfterFind AfterFind
</code></pre> </code></pre>
<h3 id="example">Example</h3> <h3 id="example">Example</h3>
<pre><code class="lang-go"><span class="token keyword">func</span> <span class="token operator">(</span>u <span class="token operator">*</span>User<span class="token operator">)</span> <span class="token function">BeforeUpdate<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">(</span>err <span class="token builtin">error</span><span class="token operator">)</span> <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">func</span> (u *User) BeforeUpdate() (err error) {
<span class="token keyword">if</span> u<span class="token operator">.</span><span class="token function">readonly<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">if</span> u.readonly() {
err <span class="token operator">=</span> errors<span class="token operator">.</span><span class="token function">New<span class="token punctuation">(</span></span><span class="token string">&quot;read only user&quot;</span><span class="token operator">)</span> err = errors.New(<span class="hljs-string">&quot;read only user&quot;</span>)
<span class="token operator">}</span> }
<span class="token keyword">return</span> <span class="hljs-keyword">return</span>
<span class="token operator">}</span> }
<span class="token comment" spellcheck="true">// Rollback the insertion if user&apos;s id greater than 1000</span> <span class="hljs-comment">// Rollback the insertion if user&apos;s id greater than 1000</span>
<span class="token keyword">func</span> <span class="token operator">(</span>u <span class="token operator">*</span>User<span class="token operator">)</span> <span class="token function">AfterCreate<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">(</span>err <span class="token builtin">error</span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">func</span> (u *User) AfterCreate() (err error) {
<span class="token keyword">if</span> <span class="token operator">(</span>u<span class="token operator">.</span>Id <span class="token operator">&gt;</span> <span class="token number">1000</span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">if</span> (u.Id &gt; <span class="hljs-number">1000</span>) {
err <span class="token operator">=</span> errors<span class="token operator">.</span><span class="token function">New<span class="token punctuation">(</span></span><span class="token string">&quot;user id is already greater than 1000&quot;</span><span class="token operator">)</span> err = errors.New(<span class="hljs-string">&quot;user id is already greater than 1000&quot;</span>)
<span class="token operator">}</span> }
<span class="token keyword">return</span> <span class="hljs-keyword">return</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
<p>Save/delete operations in gorm are running in a transaction. <p>Save/delete operations in gorm are running in a transaction.
Changes made in that transaction are not visible unless it is commited. Changes made in that transaction are not visible unless it is commited.
So if you want to use those changes in your callbacks, you need to run your SQL in the same transaction. So if you want to use those changes in your callbacks, you need to run your SQL in the same transaction.
For this Gorm supports passing transactions to callbacks like this:</p> For this Gorm supports passing transactions to callbacks like this:</p>
<pre><code class="lang-go"><span class="token keyword">func</span> <span class="token operator">(</span>u <span class="token operator">*</span>User<span class="token operator">)</span> <span class="token function">AfterCreate<span class="token punctuation">(</span></span>tx <span class="token operator">*</span>gorm<span class="token operator">.</span>DB<span class="token operator">)</span> <span class="token operator">(</span>err <span class="token builtin">error</span><span class="token operator">)</span> <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">func</span> (u *User) AfterCreate(tx *gorm.DB) (err error) {
tx<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span>u<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Update<span class="token punctuation">(</span></span><span class="token string">&quot;role&quot;</span><span class="token operator">,</span> <span class="token string">&quot;admin&quot;</span><span class="token operator">)</span> tx.Model(u).Update(<span class="hljs-string">&quot;role&quot;</span>, <span class="hljs-string">&quot;admin&quot;</span>)
<span class="token keyword">return</span> <span class="hljs-keyword">return</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
@ -704,7 +704,7 @@ For this Gorm supports passing transactions to callbacks like this:</p>
<script> <script>
require(["gitbook"], function(gitbook) { require(["gitbook"], function(gitbook) {
gitbook.start({"fontsettings":{"theme":"night","family":"sans","size":4},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"prism":{},"anker-enable":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}}); gitbook.start({"fontsettings":{"theme":"night","size":2,"family":"sans"},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"anker-enable":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}});
}); });
</script> </script>

694
curd.html
View File

@ -16,7 +16,7 @@
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css"> <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
@ -70,7 +70,7 @@
data-chapter-title="CRUD: Reading and Writing Data" data-chapter-title="CRUD: Reading and Writing Data"
data-filepath="curd.md" data-filepath="curd.md"
data-basepath="" data-basepath=""
data-revision="Mon Feb 29 2016 21:35:06 GMT+0800 (CST)" data-revision="Mon Feb 29 2016 21:51:55 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -642,531 +642,531 @@
<!-- toc stop --> <!-- toc stop -->
<h2 id="create">Create</h2> <h2 id="create">Create</h2>
<h3 id="create-record">Create Record</h3> <h3 id="create-record">Create Record</h3>
<pre><code class="lang-go">user <span class="token operator">:=</span> User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;Jinzhu&quot;</span><span class="token operator">,</span> Age<span class="token operator">:</span> <span class="token number">18</span><span class="token operator">,</span> Birthday<span class="token operator">:</span> time<span class="token operator">.</span><span class="token function">Now<span class="token punctuation">(</span></span><span class="token operator">)</span><span class="token operator">}</span> <pre><code class="lang-go">user := User{Name: <span class="hljs-string">&quot;Jinzhu&quot;</span>, Age: <span class="hljs-number">18</span>, Birthday: time.Now()}
db<span class="token operator">.</span><span class="token function">NewRecord<span class="token punctuation">(</span></span>user<span class="token operator">)</span> <span class="token comment" spellcheck="true">// =&gt; returns `true` as primary key is blank</span> db.NewRecord(user) <span class="hljs-comment">// =&gt; returns `true` as primary key is blank</span>
db<span class="token operator">.</span><span class="token function">Create<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Create(&amp;user)
db<span class="token operator">.</span><span class="token function">NewRecord<span class="token punctuation">(</span></span>user<span class="token operator">)</span> <span class="token comment" spellcheck="true">// =&gt; return `false` after `user` created</span> db.NewRecord(user) <span class="hljs-comment">// =&gt; return `false` after `user` created</span>
<span class="token comment" spellcheck="true">// Associations will be inserted automatically when save the record</span> <span class="hljs-comment">// Associations will be inserted automatically when save the record</span>
user <span class="token operator">:=</span> User<span class="token operator">{</span> user := User{
Name<span class="token operator">:</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">,</span> Name: <span class="hljs-string">&quot;jinzhu&quot;</span>,
BillingAddress<span class="token operator">:</span> Address<span class="token operator">{</span>Address1<span class="token operator">:</span> <span class="token string">&quot;Billing Address - Address 1&quot;</span><span class="token operator">}</span><span class="token operator">,</span> BillingAddress: Address{Address1: <span class="hljs-string">&quot;Billing Address - Address 1&quot;</span>},
ShippingAddress<span class="token operator">:</span> Address<span class="token operator">{</span>Address1<span class="token operator">:</span> <span class="token string">&quot;Shipping Address - Address 1&quot;</span><span class="token operator">}</span><span class="token operator">,</span> ShippingAddress: Address{Address1: <span class="hljs-string">&quot;Shipping Address - Address 1&quot;</span>},
Emails<span class="token operator">:</span> <span class="token operator">[</span><span class="token operator">]</span>Email<span class="token operator">{</span><span class="token operator">{</span>Email<span class="token operator">:</span> <span class="token string">&quot;jinzhu@example.com&quot;</span><span class="token operator">}</span><span class="token operator">,</span> <span class="token operator">{</span>Email<span class="token operator">:</span> <span class="token string">&quot;jinzhu-2@example@example.com&quot;</span><span class="token operator">}</span><span class="token operator">}</span><span class="token operator">,</span> Emails: []Email{{Email: <span class="hljs-string">&quot;jinzhu@example.com&quot;</span>}, {Email: <span class="hljs-string">&quot;jinzhu-2@example@example.com&quot;</span>}},
Languages<span class="token operator">:</span> <span class="token operator">[</span><span class="token operator">]</span>Language<span class="token operator">{</span><span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;ZH&quot;</span><span class="token operator">}</span><span class="token operator">,</span> <span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;EN&quot;</span><span class="token operator">}</span><span class="token operator">}</span><span class="token operator">,</span> Languages: []Language{{Name: <span class="hljs-string">&quot;ZH&quot;</span>}, {Name: <span class="hljs-string">&quot;EN&quot;</span>}},
<span class="token operator">}</span> }
db<span class="token operator">.</span><span class="token function">Create<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Create(&amp;user)
<span class="token comment" spellcheck="true">//// BEGIN TRANSACTION;</span> <span class="hljs-comment">//// BEGIN TRANSACTION;</span>
<span class="token comment" spellcheck="true">//// INSERT INTO &quot;addresses&quot; (address1) VALUES (&quot;Billing Address - Address 1&quot;);</span> <span class="hljs-comment">//// INSERT INTO &quot;addresses&quot; (address1) VALUES (&quot;Billing Address - Address 1&quot;);</span>
<span class="token comment" spellcheck="true">//// INSERT INTO &quot;addresses&quot; (address1) VALUES (&quot;Shipping Address - Address 1&quot;);</span> <span class="hljs-comment">//// INSERT INTO &quot;addresses&quot; (address1) VALUES (&quot;Shipping Address - Address 1&quot;);</span>
<span class="token comment" spellcheck="true">//// INSERT INTO &quot;users&quot; (name,billing_address_id,shipping_address_id) VALUES (&quot;jinzhu&quot;, 1, 2);</span> <span class="hljs-comment">//// INSERT INTO &quot;users&quot; (name,billing_address_id,shipping_address_id) VALUES (&quot;jinzhu&quot;, 1, 2);</span>
<span class="token comment" spellcheck="true">//// INSERT INTO &quot;emails&quot; (user_id,email) VALUES (111, &quot;jinzhu@example.com&quot;);</span> <span class="hljs-comment">//// INSERT INTO &quot;emails&quot; (user_id,email) VALUES (111, &quot;jinzhu@example.com&quot;);</span>
<span class="token comment" spellcheck="true">//// INSERT INTO &quot;emails&quot; (user_id,email) VALUES (111, &quot;jinzhu-2@example.com&quot;);</span> <span class="hljs-comment">//// INSERT INTO &quot;emails&quot; (user_id,email) VALUES (111, &quot;jinzhu-2@example.com&quot;);</span>
<span class="token comment" spellcheck="true">//// INSERT INTO &quot;languages&quot; (&quot;name&quot;) VALUES (&apos;ZH&apos;);</span> <span class="hljs-comment">//// INSERT INTO &quot;languages&quot; (&quot;name&quot;) VALUES (&apos;ZH&apos;);</span>
<span class="token comment" spellcheck="true">//// INSERT INTO user_languages (&quot;user_id&quot;,&quot;language_id&quot;) VALUES (111, 1);</span> <span class="hljs-comment">//// INSERT INTO user_languages (&quot;user_id&quot;,&quot;language_id&quot;) VALUES (111, 1);</span>
<span class="token comment" spellcheck="true">//// INSERT INTO &quot;languages&quot; (&quot;name&quot;) VALUES (&apos;EN&apos;);</span> <span class="hljs-comment">//// INSERT INTO &quot;languages&quot; (&quot;name&quot;) VALUES (&apos;EN&apos;);</span>
<span class="token comment" spellcheck="true">//// INSERT INTO user_languages (&quot;user_id&quot;,&quot;language_id&quot;) VALUES (111, 2);</span> <span class="hljs-comment">//// INSERT INTO user_languages (&quot;user_id&quot;,&quot;language_id&quot;) VALUES (111, 2);</span>
<span class="token comment" spellcheck="true">//// COMMIT;</span> <span class="hljs-comment">//// COMMIT;</span>
</code></pre> </code></pre>
<h3 id="create-with-associations">Create With Associations</h3> <h3 id="create-with-associations">Create With Associations</h3>
<p>Refer Associations for more details</p> <p>Refer Associations for more details</p>
<h3 id="default-values">Default Values</h3> <h3 id="default-values">Default Values</h3>
<p>You could defined default value in the <code>sql</code> tag, then the generated creating SQL will ignore these fields that including default value and its value is blank, and after inserted the record into databae, gorm will load those fields&apos;s value from database.</p> <p>You could defined default value in the <code>sql</code> tag, then the generated creating SQL will ignore these fields that including default value and its value is blank, and after inserted the record into databae, gorm will load those fields&apos;s value from database.</p>
<pre><code class="lang-go"><span class="token keyword">type</span> Animal <span class="token keyword">struct</span> <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">type</span> Animal <span class="hljs-keyword">struct</span> {
ID <span class="token builtin">int64</span> ID <span class="hljs-typename">int64</span>
Name <span class="token builtin">string</span> <span class="token string">`sql:&quot;default:&apos;galeone&apos;&quot;`</span> Name <span class="hljs-typename">string</span> <span class="hljs-string">`sql:&quot;default:&apos;galeone&apos;&quot;`</span>
Age <span class="token builtin">int64</span> Age <span class="hljs-typename">int64</span>
<span class="token operator">}</span> }
<span class="token keyword">var</span> animal <span class="token operator">=</span> Animal<span class="token operator">{</span>Age<span class="token operator">:</span> <span class="token number">99</span><span class="token operator">,</span> Name<span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token operator">}</span> <span class="hljs-keyword">var</span> animal = Animal{Age: <span class="hljs-number">99</span>, Name: <span class="hljs-string">&quot;&quot;</span>}
db<span class="token operator">.</span><span class="token function">Create<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>animal<span class="token operator">)</span> db.Create(&amp;animal)
<span class="token comment" spellcheck="true">// INSERT INTO animals(&quot;age&quot;) values(&apos;99&apos;);</span> <span class="hljs-comment">// INSERT INTO animals(&quot;age&quot;) values(&apos;99&apos;);</span>
<span class="token comment" spellcheck="true">// SELECT name from animals WHERE ID=111; // the returning primary key is 111</span> <span class="hljs-comment">// SELECT name from animals WHERE ID=111; // the returning primary key is 111</span>
<span class="token comment" spellcheck="true">// animal.Name =&gt; &apos;galeone&apos;</span> <span class="hljs-comment">// animal.Name =&gt; &apos;galeone&apos;</span>
</code></pre> </code></pre>
<h3 id="setting-primary-key-in-callbacks">Setting Primary Key In Callbacks</h3> <h3 id="setting-primary-key-in-callbacks">Setting Primary Key In Callbacks</h3>
<p>If you want to set primary key in <code>BeforeCreate</code> callback, you could use <code>scope.SetColumn</code>, for example:</p> <p>If you want to set primary key in <code>BeforeCreate</code> callback, you could use <code>scope.SetColumn</code>, for example:</p>
<pre><code class="lang-go"><span class="token keyword">func</span> <span class="token operator">(</span>user <span class="token operator">*</span>User<span class="token operator">)</span> <span class="token function">BeforeCreate<span class="token punctuation">(</span></span>scope <span class="token operator">*</span>gorm<span class="token operator">.</span>Scope<span class="token operator">)</span> <span class="token builtin">error</span> <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">func</span> (user *User) BeforeCreate(scope *gorm.Scope) error {
scope<span class="token operator">.</span><span class="token function">SetColumn<span class="token punctuation">(</span></span><span class="token string">&quot;ID&quot;</span><span class="token operator">,</span> uuid<span class="token operator">.</span><span class="token function">New<span class="token punctuation">(</span></span><span class="token operator">)</span><span class="token operator">)</span> scope.SetColumn(<span class="hljs-string">&quot;ID&quot;</span>, uuid.New())
<span class="token keyword">return</span> <span class="token boolean">nil</span> <span class="hljs-keyword">return</span> <span class="hljs-constant">nil</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h3 id="extra-creating-option">Extra Creating option</h3> <h3 id="extra-creating-option">Extra Creating option</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Add extra SQL option for inserting SQL</span> <pre><code class="lang-go"><span class="hljs-comment">// Add extra SQL option for inserting SQL</span>
db<span class="token operator">.</span><span class="token function">Set<span class="token punctuation">(</span></span><span class="token string">&quot;gorm:insert_option&quot;</span><span class="token operator">,</span> <span class="token string">&quot;ON CONFLICT&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Create<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>product<span class="token operator">)</span> db.Set(<span class="hljs-string">&quot;gorm:insert_option&quot;</span>, <span class="hljs-string">&quot;ON CONFLICT&quot;</span>).Create(&amp;product)
<span class="token comment" spellcheck="true">// INSERT INTO products (name, code) VALUES (&quot;name&quot;, &quot;code&quot;) ON CONFLICT;</span> <span class="hljs-comment">// INSERT INTO products (name, code) VALUES (&quot;name&quot;, &quot;code&quot;) ON CONFLICT;</span>
</code></pre> </code></pre>
<h2 id="query">Query</h2> <h2 id="query">Query</h2>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Get the first record</span> <pre><code class="lang-go"><span class="hljs-comment">// Get the first record</span>
db<span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.First(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users ORDER BY id LIMIT 1;</span> <span class="hljs-comment">//// SELECT * FROM users ORDER BY id LIMIT 1;</span>
<span class="token comment" spellcheck="true">// Get the last record</span> <span class="hljs-comment">// Get the last record</span>
db<span class="token operator">.</span><span class="token function">Last<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Last(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users ORDER BY id DESC LIMIT 1;</span> <span class="hljs-comment">//// SELECT * FROM users ORDER BY id DESC LIMIT 1;</span>
<span class="token comment" spellcheck="true">// Get all records</span> <span class="hljs-comment">// Get all records</span>
db<span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users;</span> <span class="hljs-comment">//// SELECT * FROM users;</span>
<span class="token comment" spellcheck="true">// Get record with primary key</span> <span class="hljs-comment">// Get record with primary key</span>
db<span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">,</span> <span class="token number">10</span><span class="token operator">)</span> db.First(&amp;user, <span class="hljs-number">10</span>)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE id = 10;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE id = 10;</span>
</code></pre> </code></pre>
<h3 id="query-with-where-plain-sql">Query With Where (Plain SQL)</h3> <h3 id="query-with-where-plain-sql">Query With Where (Plain SQL)</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Get the first matched record</span> <pre><code class="lang-go"><span class="hljs-comment">// Get the first matched record</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).First(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name = &apos;jinzhu&apos; limit 1;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name = &apos;jinzhu&apos; limit 1;</span>
<span class="token comment" spellcheck="true">// Get all matched records</span> <span class="hljs-comment">// Get all matched records</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name = &apos;jinzhu&apos;;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name = &apos;jinzhu&apos;;</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name &lt;&gt; ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;name &lt;&gt; ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">// IN</span> <span class="hljs-comment">// IN</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name in (?)&quot;</span><span class="token operator">,</span> <span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">string</span><span class="token operator">{</span><span class="token string">&quot;jinzhu&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu 2&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;name in (?)&quot;</span>, []<span class="hljs-typename">string</span>{<span class="hljs-string">&quot;jinzhu&quot;</span>, <span class="hljs-string">&quot;jinzhu 2&quot;</span>}).Find(&amp;users)
<span class="token comment" spellcheck="true">// LIKE</span> <span class="hljs-comment">// LIKE</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name LIKE ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;%jin%&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;name LIKE ?&quot;</span>, <span class="hljs-string">&quot;%jin%&quot;</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">// AND</span> <span class="hljs-comment">// AND</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ? and age &gt;= ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">,</span> <span class="token string">&quot;22&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;name = ? and age &gt;= ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>, <span class="hljs-string">&quot;22&quot;</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">// Time</span> <span class="hljs-comment">// Time</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;updated_at &gt; ?&quot;</span><span class="token operator">,</span> lastWeek<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;updated_at &gt; ?&quot;</span>, lastWeek).Find(&amp;users)
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;created_at BETWEEN ? AND ?&quot;</span><span class="token operator">,</span> lastWeek<span class="token operator">,</span> today<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;created_at BETWEEN ? AND ?&quot;</span>, lastWeek, today).Find(&amp;users)
</code></pre> </code></pre>
<h3 id="query-with-where-struct--map">Query With Where (Struct &amp; Map)</h3> <h3 id="query-with-where-struct--map">Query With Where (Struct &amp; Map)</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Struct</span> <pre><code class="lang-go"><span class="hljs-comment">// Struct</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">,</span> Age<span class="token operator">:</span> <span class="token number">20</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(&amp;User{Name: <span class="hljs-string">&quot;jinzhu&quot;</span>, Age: <span class="hljs-number">20</span>}).First(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name = &quot;jinzhu&quot; AND age = 20 LIMIT 1;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name = &quot;jinzhu&quot; AND age = 20 LIMIT 1;</span>
<span class="token comment" spellcheck="true">// Map</span> <span class="hljs-comment">// Map</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token keyword">map</span><span class="token operator">[</span><span class="token builtin">string</span><span class="token operator">]</span><span class="token keyword">interface</span><span class="token operator">{</span><span class="token operator">}</span><span class="token operator">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">,</span> <span class="token string">&quot;age&quot;</span><span class="token operator">:</span> <span class="token number">20</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where(<span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]<span class="hljs-keyword">interface</span>{}{<span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;jinzhu&quot;</span>, <span class="hljs-string">&quot;age&quot;</span>: <span class="hljs-number">20</span>}).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name = &quot;jinzhu&quot; AND age = 20;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name = &quot;jinzhu&quot; AND age = 20;</span>
<span class="token comment" spellcheck="true">// Slice of primary keys</span> <span class="hljs-comment">// Slice of primary keys</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">int64</span><span class="token operator">{</span><span class="token number">20</span><span class="token operator">,</span> <span class="token number">21</span><span class="token operator">,</span> <span class="token number">22</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where([]<span class="hljs-typename">int64</span>{<span class="hljs-number">20</span>, <span class="hljs-number">21</span>, <span class="hljs-number">22</span>}).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE id IN (20, 21, 22);</span> <span class="hljs-comment">//// SELECT * FROM users WHERE id IN (20, 21, 22);</span>
</code></pre> </code></pre>
<h3 id="query-with-not">Query With Not</h3> <h3 id="query-with-not">Query With Not</h3>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Not<span class="token punctuation">(</span></span><span class="token string">&quot;name&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> <pre><code class="lang-go">db.Not(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).First(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name &lt;&gt; &quot;jinzhu&quot; LIMIT 1;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name &lt;&gt; &quot;jinzhu&quot; LIMIT 1;</span>
<span class="token comment" spellcheck="true">// Not In</span> <span class="hljs-comment">// Not In</span>
db<span class="token operator">.</span><span class="token function">Not<span class="token punctuation">(</span></span><span class="token string">&quot;name&quot;</span><span class="token operator">,</span> <span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">string</span><span class="token operator">{</span><span class="token string">&quot;jinzhu&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu 2&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Not(<span class="hljs-string">&quot;name&quot;</span>, []<span class="hljs-typename">string</span>{<span class="hljs-string">&quot;jinzhu&quot;</span>, <span class="hljs-string">&quot;jinzhu 2&quot;</span>}).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name NOT IN (&quot;jinzhu&quot;, &quot;jinzhu 2&quot;);</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name NOT IN (&quot;jinzhu&quot;, &quot;jinzhu 2&quot;);</span>
<span class="token comment" spellcheck="true">// Not In slice of primary keys</span> <span class="hljs-comment">// Not In slice of primary keys</span>
db<span class="token operator">.</span><span class="token function">Not<span class="token punctuation">(</span></span><span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">int64</span><span class="token operator">{</span><span class="token number">1</span><span class="token operator">,</span><span class="token number">2</span><span class="token operator">,</span><span class="token number">3</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Not([]<span class="hljs-typename">int64</span>{<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>}).First(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE id NOT IN (1,2,3);</span> <span class="hljs-comment">//// SELECT * FROM users WHERE id NOT IN (1,2,3);</span>
db<span class="token operator">.</span><span class="token function">Not<span class="token punctuation">(</span></span><span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">int64</span><span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Not([]<span class="hljs-typename">int64</span>{}).First(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users;</span> <span class="hljs-comment">//// SELECT * FROM users;</span>
<span class="token comment" spellcheck="true">// Plain SQL</span> <span class="hljs-comment">// Plain SQL</span>
db<span class="token operator">.</span><span class="token function">Not<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Not(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).First(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE NOT(name = &quot;jinzhu&quot;);</span> <span class="hljs-comment">//// SELECT * FROM users WHERE NOT(name = &quot;jinzhu&quot;);</span>
<span class="token comment" spellcheck="true">// Struct</span> <span class="hljs-comment">// Struct</span>
db<span class="token operator">.</span><span class="token function">Not<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Not(User{Name: <span class="hljs-string">&quot;jinzhu&quot;</span>}).First(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name &lt;&gt; &quot;jinzhu&quot;;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name &lt;&gt; &quot;jinzhu&quot;;</span>
</code></pre> </code></pre>
<h3 id="query-with-inline-condition">Query With Inline Condition</h3> <h3 id="query-with-inline-condition">Query With Inline Condition</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Get by primary key</span> <pre><code class="lang-go"><span class="hljs-comment">// Get by primary key</span>
db<span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">,</span> <span class="token number">23</span><span class="token operator">)</span> db.First(&amp;user, <span class="hljs-number">23</span>)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE id = 23 LIMIT 1;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE id = 23 LIMIT 1;</span>
<span class="token comment" spellcheck="true">// Plain SQL</span> <span class="hljs-comment">// Plain SQL</span>
db<span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">,</span> <span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span> db.Find(&amp;user, <span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name = &quot;jinzhu&quot;;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name = &quot;jinzhu&quot;;</span>
db<span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">,</span> <span class="token string">&quot;name &lt;&gt; ? AND age &gt; ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">,</span> <span class="token number">20</span><span class="token operator">)</span> db.Find(&amp;users, <span class="hljs-string">&quot;name &lt;&gt; ? AND age &gt; ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>, <span class="hljs-number">20</span>)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name &lt;&gt; &quot;jinzhu&quot; AND age &gt; 20;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name &lt;&gt; &quot;jinzhu&quot; AND age &gt; 20;</span>
<span class="token comment" spellcheck="true">// Struct</span> <span class="hljs-comment">// Struct</span>
db<span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">,</span> User<span class="token operator">{</span>Age<span class="token operator">:</span> <span class="token number">20</span><span class="token operator">}</span><span class="token operator">)</span> db.Find(&amp;users, User{Age: <span class="hljs-number">20</span>})
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE age = 20;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE age = 20;</span>
<span class="token comment" spellcheck="true">// Map</span> <span class="hljs-comment">// Map</span>
db<span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">,</span> <span class="token keyword">map</span><span class="token operator">[</span><span class="token builtin">string</span><span class="token operator">]</span><span class="token keyword">interface</span><span class="token operator">{</span><span class="token operator">}</span><span class="token operator">{</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span> <span class="token number">20</span><span class="token operator">}</span><span class="token operator">)</span> db.Find(&amp;users, <span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]<span class="hljs-keyword">interface</span>{}{<span class="hljs-string">&quot;age&quot;</span>: <span class="hljs-number">20</span>})
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE age = 20;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE age = 20;</span>
</code></pre> </code></pre>
<h3 id="query-with-or">Query With Or</h3> <h3 id="query-with-or">Query With Or</h3>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;role = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;admin&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Or<span class="token punctuation">(</span></span><span class="token string">&quot;role = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;super_admin&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> <pre><code class="lang-go">db.Where(<span class="hljs-string">&quot;role = ?&quot;</span>, <span class="hljs-string">&quot;admin&quot;</span>).Or(<span class="hljs-string">&quot;role = ?&quot;</span>, <span class="hljs-string">&quot;super_admin&quot;</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE role = &apos;admin&apos; OR role = &apos;super_admin&apos;;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE role = &apos;admin&apos; OR role = &apos;super_admin&apos;;</span>
<span class="token comment" spellcheck="true">// Struct</span> <span class="hljs-comment">// Struct</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = &apos;jinzhu&apos;&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Or<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;jinzhu 2&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;name = &apos;jinzhu&apos;&quot;</span>).Or(User{Name: <span class="hljs-string">&quot;jinzhu 2&quot;</span>}).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name = &apos;jinzhu&apos; OR name = &apos;jinzhu 2&apos;;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name = &apos;jinzhu&apos; OR name = &apos;jinzhu 2&apos;;</span>
<span class="token comment" spellcheck="true">// Map</span> <span class="hljs-comment">// Map</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = &apos;jinzhu&apos;&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Or<span class="token punctuation">(</span></span><span class="token keyword">map</span><span class="token operator">[</span><span class="token builtin">string</span><span class="token operator">]</span><span class="token keyword">interface</span><span class="token operator">{</span><span class="token operator">}</span><span class="token operator">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;jinzhu 2&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;name = &apos;jinzhu&apos;&quot;</span>).Or(<span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]<span class="hljs-keyword">interface</span>{}{<span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;jinzhu 2&quot;</span>}).Find(&amp;users)
</code></pre> </code></pre>
<h3 id="query-chains">Query Chains</h3> <h3 id="query-chains">Query Chains</h3>
<p>Gorm has a chainable API, you could use it like this</p> <p>Gorm has a chainable API, you could use it like this</p>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name &lt;&gt; ?&quot;</span><span class="token operator">,</span><span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;age &gt;= ? and role &lt;&gt; ?&quot;</span><span class="token operator">,</span><span class="token number">20</span><span class="token operator">,</span><span class="token string">&quot;admin&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> <pre><code class="lang-go">db.Where(<span class="hljs-string">&quot;name &lt;&gt; ?&quot;</span>,<span class="hljs-string">&quot;jinzhu&quot;</span>).Where(<span class="hljs-string">&quot;age &gt;= ? and role &lt;&gt; ?&quot;</span>,<span class="hljs-number">20</span>,<span class="hljs-string">&quot;admin&quot;</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name &lt;&gt; &apos;jinzhu&apos; AND age &gt;= 20 AND role &lt;&gt; &apos;admin&apos;;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name &lt;&gt; &apos;jinzhu&apos; AND age &gt;= 20 AND role &lt;&gt; &apos;admin&apos;;</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;role = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;admin&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Or<span class="token punctuation">(</span></span><span class="token string">&quot;role = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;super_admin&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Not<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;role = ?&quot;</span>, <span class="hljs-string">&quot;admin&quot;</span>).Or(<span class="hljs-string">&quot;role = ?&quot;</span>, <span class="hljs-string">&quot;super_admin&quot;</span>).Not(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Find(&amp;users)
</code></pre> </code></pre>
<h3 id="extra-querying-option">Extra Querying option</h3> <h3 id="extra-querying-option">Extra Querying option</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Add extra SQL option for selecting SQL</span> <pre><code class="lang-go"><span class="hljs-comment">// Add extra SQL option for selecting SQL</span>
db<span class="token operator">.</span><span class="token function">Set<span class="token punctuation">(</span></span><span class="token string">&quot;gorm:query_option&quot;</span><span class="token operator">,</span> <span class="token string">&quot;FOR UPDATE&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">,</span> <span class="token number">10</span><span class="token operator">)</span> db.Set(<span class="hljs-string">&quot;gorm:query_option&quot;</span>, <span class="hljs-string">&quot;FOR UPDATE&quot;</span>).First(&amp;user, <span class="hljs-number">10</span>)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE id = 10 FOR UPDATE;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE id = 10 FOR UPDATE;</span>
</code></pre> </code></pre>
<h3 id="firstorinit">FirstOrInit</h3> <h3 id="firstorinit">FirstOrInit</h3>
<p>Get the first matched record, or initialize a record with search conditions.</p> <p>Get the first matched record, or initialize a record with search conditions.</p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Unfound</span> <pre><code class="lang-go"><span class="hljs-comment">// Unfound</span>
db<span class="token operator">.</span><span class="token function">FirstOrInit<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">,</span> User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;non_existing&quot;</span><span class="token operator">}</span><span class="token operator">)</span> db.FirstOrInit(&amp;user, User{Name: <span class="hljs-string">&quot;non_existing&quot;</span>})
<span class="token comment" spellcheck="true">//// user -&gt; User{Name: &quot;non_existing&quot;}</span> <span class="hljs-comment">//// user -&gt; User{Name: &quot;non_existing&quot;}</span>
<span class="token comment" spellcheck="true">// Found</span> <span class="hljs-comment">// Found</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;Jinzhu&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">FirstOrInit<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(User{Name: <span class="hljs-string">&quot;Jinzhu&quot;</span>}).FirstOrInit(&amp;user)
<span class="token comment" spellcheck="true">//// user -&gt; User{Id: 111, Name: &quot;Jinzhu&quot;, Age: 20}</span> <span class="hljs-comment">//// user -&gt; User{Id: 111, Name: &quot;Jinzhu&quot;, Age: 20}</span>
db<span class="token operator">.</span><span class="token function">FirstOrInit<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">,</span> <span class="token keyword">map</span><span class="token operator">[</span><span class="token builtin">string</span><span class="token operator">]</span><span class="token keyword">interface</span><span class="token operator">{</span><span class="token operator">}</span><span class="token operator">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">}</span><span class="token operator">)</span> db.FirstOrInit(&amp;user, <span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]<span class="hljs-keyword">interface</span>{}{<span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;jinzhu&quot;</span>})
<span class="token comment" spellcheck="true">//// user -&gt; User{Id: 111, Name: &quot;Jinzhu&quot;, Age: 20}</span> <span class="hljs-comment">//// user -&gt; User{Id: 111, Name: &quot;Jinzhu&quot;, Age: 20}</span>
</code></pre> </code></pre>
<h4 id="attrs">Attrs</h4> <h4 id="attrs">Attrs</h4>
<p>Ignore some values when searching, but use them to initialize the struct if record is not found.</p> <p>Ignore some values when searching, but use them to initialize the struct if record is not found.</p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Unfound</span> <pre><code class="lang-go"><span class="hljs-comment">// Unfound</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;non_existing&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Attrs<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Age<span class="token operator">:</span> <span class="token number">20</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">FirstOrInit<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(User{Name: <span class="hljs-string">&quot;non_existing&quot;</span>}).Attrs(User{Age: <span class="hljs-number">20</span>}).FirstOrInit(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM USERS WHERE name = &apos;non_existing&apos;;</span> <span class="hljs-comment">//// SELECT * FROM USERS WHERE name = &apos;non_existing&apos;;</span>
<span class="token comment" spellcheck="true">//// user -&gt; User{Name: &quot;non_existing&quot;, Age: 20}</span> <span class="hljs-comment">//// user -&gt; User{Name: &quot;non_existing&quot;, Age: 20}</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;noexisting_user&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Attrs<span class="token punctuation">(</span></span><span class="token string">&quot;age&quot;</span><span class="token operator">,</span> <span class="token number">20</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">FirstOrInit<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(User{Name: <span class="hljs-string">&quot;noexisting_user&quot;</span>}).Attrs(<span class="hljs-string">&quot;age&quot;</span>, <span class="hljs-number">20</span>).FirstOrInit(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM USERS WHERE name = &apos;non_existing&apos;;</span> <span class="hljs-comment">//// SELECT * FROM USERS WHERE name = &apos;non_existing&apos;;</span>
<span class="token comment" spellcheck="true">//// user -&gt; User{Name: &quot;non_existing&quot;, Age: 20}</span> <span class="hljs-comment">//// user -&gt; User{Name: &quot;non_existing&quot;, Age: 20}</span>
<span class="token comment" spellcheck="true">// Found</span> <span class="hljs-comment">// Found</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;Jinzhu&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Attrs<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Age<span class="token operator">:</span> <span class="token number">30</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">FirstOrInit<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(User{Name: <span class="hljs-string">&quot;Jinzhu&quot;</span>}).Attrs(User{Age: <span class="hljs-number">30</span>}).FirstOrInit(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM USERS WHERE name = jinzhu&apos;;</span> <span class="hljs-comment">//// SELECT * FROM USERS WHERE name = jinzhu&apos;;</span>
<span class="token comment" spellcheck="true">//// user -&gt; User{Id: 111, Name: &quot;Jinzhu&quot;, Age: 20}</span> <span class="hljs-comment">//// user -&gt; User{Id: 111, Name: &quot;Jinzhu&quot;, Age: 20}</span>
</code></pre> </code></pre>
<h4 id="assign">Assign</h4> <h4 id="assign">Assign</h4>
<p>Ignore some values when searching, but assign it to the result regardless it is found or not.</p> <p>Ignore some values when searching, but assign it to the result regardless it is found or not.</p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Unfound</span> <pre><code class="lang-go"><span class="hljs-comment">// Unfound</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;non_existing&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Assign<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Age<span class="token operator">:</span> <span class="token number">20</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">FirstOrInit<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(User{Name: <span class="hljs-string">&quot;non_existing&quot;</span>}).Assign(User{Age: <span class="hljs-number">20</span>}).FirstOrInit(&amp;user)
<span class="token comment" spellcheck="true">//// user -&gt; User{Name: &quot;non_existing&quot;, Age: 20}</span> <span class="hljs-comment">//// user -&gt; User{Name: &quot;non_existing&quot;, Age: 20}</span>
<span class="token comment" spellcheck="true">// Found</span> <span class="hljs-comment">// Found</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;Jinzhu&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Assign<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Age<span class="token operator">:</span> <span class="token number">30</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">FirstOrInit<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(User{Name: <span class="hljs-string">&quot;Jinzhu&quot;</span>}).Assign(User{Age: <span class="hljs-number">30</span>}).FirstOrInit(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM USERS WHERE name = jinzhu&apos;;</span> <span class="hljs-comment">//// SELECT * FROM USERS WHERE name = jinzhu&apos;;</span>
<span class="token comment" spellcheck="true">//// user -&gt; User{Id: 111, Name: &quot;Jinzhu&quot;, Age: 30}</span> <span class="hljs-comment">//// user -&gt; User{Id: 111, Name: &quot;Jinzhu&quot;, Age: 30}</span>
</code></pre> </code></pre>
<h3 id="firstorcreate">FirstOrCreate</h3> <h3 id="firstorcreate">FirstOrCreate</h3>
<p>Get the first matched record, or create with search conditions.</p> <p>Get the first matched record, or create with search conditions.</p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Unfound</span> <pre><code class="lang-go"><span class="hljs-comment">// Unfound</span>
db<span class="token operator">.</span><span class="token function">FirstOrCreate<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">,</span> User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;non_existing&quot;</span><span class="token operator">}</span><span class="token operator">)</span> db.FirstOrCreate(&amp;user, User{Name: <span class="hljs-string">&quot;non_existing&quot;</span>})
<span class="token comment" spellcheck="true">//// INSERT INTO &quot;users&quot; (name) VALUES (&quot;non_existing&quot;);</span> <span class="hljs-comment">//// INSERT INTO &quot;users&quot; (name) VALUES (&quot;non_existing&quot;);</span>
<span class="token comment" spellcheck="true">//// user -&gt; User{Id: 112, Name: &quot;non_existing&quot;}</span> <span class="hljs-comment">//// user -&gt; User{Id: 112, Name: &quot;non_existing&quot;}</span>
<span class="token comment" spellcheck="true">// Found</span> <span class="hljs-comment">// Found</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;Jinzhu&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">FirstOrCreate<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(User{Name: <span class="hljs-string">&quot;Jinzhu&quot;</span>}).FirstOrCreate(&amp;user)
<span class="token comment" spellcheck="true">//// user -&gt; User{Id: 111, Name: &quot;Jinzhu&quot;}</span> <span class="hljs-comment">//// user -&gt; User{Id: 111, Name: &quot;Jinzhu&quot;}</span>
</code></pre> </code></pre>
<h4 id="attrs">Attrs</h4> <h4 id="attrs">Attrs</h4>
<p>Ignore some values when searching, but use them to create the struct if record is not found. like <code>FirstOrInit</code></p> <p>Ignore some values when searching, but use them to create the struct if record is not found. like <code>FirstOrInit</code></p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Unfound</span> <pre><code class="lang-go"><span class="hljs-comment">// Unfound</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;non_existing&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Attrs<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Age<span class="token operator">:</span> <span class="token number">20</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">FirstOrCreate<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(User{Name: <span class="hljs-string">&quot;non_existing&quot;</span>}).Attrs(User{Age: <span class="hljs-number">20</span>}).FirstOrCreate(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name = &apos;non_existing&apos;;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name = &apos;non_existing&apos;;</span>
<span class="token comment" spellcheck="true">//// INSERT INTO &quot;users&quot; (name, age) VALUES (&quot;non_existing&quot;, 20);</span> <span class="hljs-comment">//// INSERT INTO &quot;users&quot; (name, age) VALUES (&quot;non_existing&quot;, 20);</span>
<span class="token comment" spellcheck="true">//// user -&gt; User{Id: 112, Name: &quot;non_existing&quot;, Age: 20}</span> <span class="hljs-comment">//// user -&gt; User{Id: 112, Name: &quot;non_existing&quot;, Age: 20}</span>
<span class="token comment" spellcheck="true">// Found</span> <span class="hljs-comment">// Found</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Attrs<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Age<span class="token operator">:</span> <span class="token number">30</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">FirstOrCreate<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(User{Name: <span class="hljs-string">&quot;jinzhu&quot;</span>}).Attrs(User{Age: <span class="hljs-number">30</span>}).FirstOrCreate(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name = &apos;jinzhu&apos;;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name = &apos;jinzhu&apos;;</span>
<span class="token comment" spellcheck="true">//// user -&gt; User{Id: 111, Name: &quot;jinzhu&quot;, Age: 20}</span> <span class="hljs-comment">//// user -&gt; User{Id: 111, Name: &quot;jinzhu&quot;, Age: 20}</span>
</code></pre> </code></pre>
<h4 id="assign">Assign</h4> <h4 id="assign">Assign</h4>
<p>Ignore some values when searching, but assign it to the record regardless it is found or not, then save back to database. like <code>FirstOrInit</code></p> <p>Ignore some values when searching, but assign it to the record regardless it is found or not, then save back to database. like <code>FirstOrInit</code></p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Unfound</span> <pre><code class="lang-go"><span class="hljs-comment">// Unfound</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;non_existing&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Assign<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Age<span class="token operator">:</span> <span class="token number">20</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">FirstOrCreate<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(User{Name: <span class="hljs-string">&quot;non_existing&quot;</span>}).Assign(User{Age: <span class="hljs-number">20</span>}).FirstOrCreate(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name = &apos;non_existing&apos;;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name = &apos;non_existing&apos;;</span>
<span class="token comment" spellcheck="true">//// INSERT INTO &quot;users&quot; (name, age) VALUES (&quot;non_existing&quot;, 20);</span> <span class="hljs-comment">//// INSERT INTO &quot;users&quot; (name, age) VALUES (&quot;non_existing&quot;, 20);</span>
<span class="token comment" spellcheck="true">//// user -&gt; User{Id: 112, Name: &quot;non_existing&quot;, Age: 20}</span> <span class="hljs-comment">//// user -&gt; User{Id: 112, Name: &quot;non_existing&quot;, Age: 20}</span>
<span class="token comment" spellcheck="true">// Found</span> <span class="hljs-comment">// Found</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Assign<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Age<span class="token operator">:</span> <span class="token number">30</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">FirstOrCreate<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(User{Name: <span class="hljs-string">&quot;jinzhu&quot;</span>}).Assign(User{Age: <span class="hljs-number">30</span>}).FirstOrCreate(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE name = &apos;jinzhu&apos;;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE name = &apos;jinzhu&apos;;</span>
<span class="token comment" spellcheck="true">//// UPDATE users SET age=30 WHERE id = 111;</span> <span class="hljs-comment">//// UPDATE users SET age=30 WHERE id = 111;</span>
<span class="token comment" spellcheck="true">//// user -&gt; User{Id: 111, Name: &quot;jinzhu&quot;, Age: 30}</span> <span class="hljs-comment">//// user -&gt; User{Id: 111, Name: &quot;jinzhu&quot;, Age: 30}</span>
</code></pre> </code></pre>
<h3 id="select">Select</h3> <h3 id="select">Select</h3>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;name, age&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> <pre><code class="lang-go">db.Select(<span class="hljs-string">&quot;name, age&quot;</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT name, age FROM users;</span> <span class="hljs-comment">//// SELECT name, age FROM users;</span>
db<span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">string</span><span class="token operator">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">,</span> <span class="token string">&quot;age&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Select([]<span class="hljs-typename">string</span>{<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;age&quot;</span>}).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT name, age FROM users;</span> <span class="hljs-comment">//// SELECT name, age FROM users;</span>
db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;users&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;COALESCE(age,?)&quot;</span><span class="token operator">,</span> <span class="token number">42</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Rows<span class="token punctuation">(</span></span><span class="token operator">)</span> db.Table(<span class="hljs-string">&quot;users&quot;</span>).Select(<span class="hljs-string">&quot;COALESCE(age,?)&quot;</span>, <span class="hljs-number">42</span>).Rows()
<span class="token comment" spellcheck="true">//// SELECT COALESCE(age,&apos;42&apos;) FROM users;</span> <span class="hljs-comment">//// SELECT COALESCE(age,&apos;42&apos;) FROM users;</span>
</code></pre> </code></pre>
<h3 id="order">Order</h3> <h3 id="order">Order</h3>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Order<span class="token punctuation">(</span></span><span class="token string">&quot;age desc, name&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> <pre><code class="lang-go">db.Order(<span class="hljs-string">&quot;age desc, name&quot;</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users ORDER BY age desc, name;</span> <span class="hljs-comment">//// SELECT * FROM users ORDER BY age desc, name;</span>
<span class="token comment" spellcheck="true">// Multiple orders</span> <span class="hljs-comment">// Multiple orders</span>
db<span class="token operator">.</span><span class="token function">Order<span class="token punctuation">(</span></span><span class="token string">&quot;age desc&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Order<span class="token punctuation">(</span></span><span class="token string">&quot;name&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Order(<span class="hljs-string">&quot;age desc&quot;</span>).Order(<span class="hljs-string">&quot;name&quot;</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users ORDER BY age desc, name;</span> <span class="hljs-comment">//// SELECT * FROM users ORDER BY age desc, name;</span>
<span class="token comment" spellcheck="true">// ReOrder</span> <span class="hljs-comment">// ReOrder</span>
db<span class="token operator">.</span><span class="token function">Order<span class="token punctuation">(</span></span><span class="token string">&quot;age desc&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users1<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Order<span class="token punctuation">(</span></span><span class="token string">&quot;age&quot;</span><span class="token operator">,</span> <span class="token boolean">true</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users2<span class="token operator">)</span> db.Order(<span class="hljs-string">&quot;age desc&quot;</span>).Find(&amp;users1).Order(<span class="hljs-string">&quot;age&quot;</span>, <span class="hljs-constant">true</span>).Find(&amp;users2)
<span class="token comment" spellcheck="true">//// SELECT * FROM users ORDER BY age desc; (users1)</span> <span class="hljs-comment">//// SELECT * FROM users ORDER BY age desc; (users1)</span>
<span class="token comment" spellcheck="true">//// SELECT * FROM users ORDER BY age; (users2)</span> <span class="hljs-comment">//// SELECT * FROM users ORDER BY age; (users2)</span>
</code></pre> </code></pre>
<h3 id="limit">Limit</h3> <h3 id="limit">Limit</h3>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Limit<span class="token punctuation">(</span></span><span class="token number">3</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> <pre><code class="lang-go">db.Limit(<span class="hljs-number">3</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users LIMIT 3;</span> <span class="hljs-comment">//// SELECT * FROM users LIMIT 3;</span>
<span class="token comment" spellcheck="true">// Cancel limit condition with -1</span> <span class="hljs-comment">// Cancel limit condition with -1</span>
db<span class="token operator">.</span><span class="token function">Limit<span class="token punctuation">(</span></span><span class="token number">10</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users1<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Limit<span class="token punctuation">(</span></span><span class="token operator">-</span><span class="token number">1</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users2<span class="token operator">)</span> db.Limit(<span class="hljs-number">10</span>).Find(&amp;users1).Limit(-<span class="hljs-number">1</span>).Find(&amp;users2)
<span class="token comment" spellcheck="true">//// SELECT * FROM users LIMIT 10; (users1)</span> <span class="hljs-comment">//// SELECT * FROM users LIMIT 10; (users1)</span>
<span class="token comment" spellcheck="true">//// SELECT * FROM users; (users2)</span> <span class="hljs-comment">//// SELECT * FROM users; (users2)</span>
</code></pre> </code></pre>
<h3 id="offset">Offset</h3> <h3 id="offset">Offset</h3>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Offset<span class="token punctuation">(</span></span><span class="token number">3</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> <pre><code class="lang-go">db.Offset(<span class="hljs-number">3</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users OFFSET 3;</span> <span class="hljs-comment">//// SELECT * FROM users OFFSET 3;</span>
<span class="token comment" spellcheck="true">// Cancel offset condition with -1</span> <span class="hljs-comment">// Cancel offset condition with -1</span>
db<span class="token operator">.</span><span class="token function">Offset<span class="token punctuation">(</span></span><span class="token number">10</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users1<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Offset<span class="token punctuation">(</span></span><span class="token operator">-</span><span class="token number">1</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users2<span class="token operator">)</span> db.Offset(<span class="hljs-number">10</span>).Find(&amp;users1).Offset(-<span class="hljs-number">1</span>).Find(&amp;users2)
<span class="token comment" spellcheck="true">//// SELECT * FROM users OFFSET 10; (users1)</span> <span class="hljs-comment">//// SELECT * FROM users OFFSET 10; (users1)</span>
<span class="token comment" spellcheck="true">//// SELECT * FROM users; (users2)</span> <span class="hljs-comment">//// SELECT * FROM users; (users2)</span>
</code></pre> </code></pre>
<h3 id="count">Count</h3> <h3 id="count">Count</h3>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Or<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu 2&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Count<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>count<span class="token operator">)</span> <pre><code class="lang-go">db.Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Or(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu 2&quot;</span>).Find(&amp;users).Count(&amp;count)
<span class="token comment" spellcheck="true">//// SELECT * from USERS WHERE name = &apos;jinzhu&apos; OR name = &apos;jinzhu 2&apos;; (users)</span> <span class="hljs-comment">//// SELECT * from USERS WHERE name = &apos;jinzhu&apos; OR name = &apos;jinzhu 2&apos;; (users)</span>
<span class="token comment" spellcheck="true">//// SELECT count(*) FROM users WHERE name = &apos;jinzhu&apos; OR name = &apos;jinzhu 2&apos;; (count)</span> <span class="hljs-comment">//// SELECT count(*) FROM users WHERE name = &apos;jinzhu&apos; OR name = &apos;jinzhu 2&apos;; (count)</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Count<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>count<span class="token operator">)</span> db.Model(&amp;User{}).Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Count(&amp;count)
<span class="token comment" spellcheck="true">//// SELECT count(*) FROM users WHERE name = &apos;jinzhu&apos;; (count)</span> <span class="hljs-comment">//// SELECT count(*) FROM users WHERE name = &apos;jinzhu&apos;; (count)</span>
db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;deleted_users&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Count<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>count<span class="token operator">)</span> db.Table(<span class="hljs-string">&quot;deleted_users&quot;</span>).Count(&amp;count)
<span class="token comment" spellcheck="true">//// SELECT count(*) FROM deleted_users;</span> <span class="hljs-comment">//// SELECT count(*) FROM deleted_users;</span>
</code></pre> </code></pre>
<h3 id="group--having">Group &amp; Having</h3> <h3 id="group--having">Group &amp; Having</h3>
<pre><code class="lang-go">rows<span class="token operator">,</span> err <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;orders&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;date(created_at) as date, sum(amount) as total&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Group<span class="token punctuation">(</span></span><span class="token string">&quot;date(created_at)&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Rows<span class="token punctuation">(</span></span><span class="token operator">)</span> <pre><code class="lang-go">rows, err := db.Table(<span class="hljs-string">&quot;orders&quot;</span>).Select(<span class="hljs-string">&quot;date(created_at) as date, sum(amount) as total&quot;</span>).Group(<span class="hljs-string">&quot;date(created_at)&quot;</span>).Rows()
<span class="token keyword">for</span> rows<span class="token operator">.</span><span class="token function">Next<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">for</span> rows.Next() {
<span class="token operator">...</span> ...
<span class="token operator">}</span> }
rows<span class="token operator">,</span> err <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;orders&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;date(created_at) as date, sum(amount) as total&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Group<span class="token punctuation">(</span></span><span class="token string">&quot;date(created_at)&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Having<span class="token punctuation">(</span></span><span class="token string">&quot;sum(amount) &gt; ?&quot;</span><span class="token operator">,</span> <span class="token number">100</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Rows<span class="token punctuation">(</span></span><span class="token operator">)</span> rows, err := db.Table(<span class="hljs-string">&quot;orders&quot;</span>).Select(<span class="hljs-string">&quot;date(created_at) as date, sum(amount) as total&quot;</span>).Group(<span class="hljs-string">&quot;date(created_at)&quot;</span>).Having(<span class="hljs-string">&quot;sum(amount) &gt; ?&quot;</span>, <span class="hljs-number">100</span>).Rows()
<span class="token keyword">for</span> rows<span class="token operator">.</span><span class="token function">Next<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">for</span> rows.Next() {
<span class="token operator">...</span> ...
<span class="token operator">}</span> }
<span class="token keyword">type</span> Result <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> Result <span class="hljs-keyword">struct</span> {
Date time<span class="token operator">.</span>Time Date time.Time
Total <span class="token builtin">int64</span> Total <span class="hljs-typename">int64</span>
<span class="token operator">}</span> }
db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;orders&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;date(created_at) as date, sum(amount) as total&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Group<span class="token punctuation">(</span></span><span class="token string">&quot;date(created_at)&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Having<span class="token punctuation">(</span></span><span class="token string">&quot;sum(amount) &gt; ?&quot;</span><span class="token operator">,</span> <span class="token number">100</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Scan<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>results<span class="token operator">)</span> db.Table(<span class="hljs-string">&quot;orders&quot;</span>).Select(<span class="hljs-string">&quot;date(created_at) as date, sum(amount) as total&quot;</span>).Group(<span class="hljs-string">&quot;date(created_at)&quot;</span>).Having(<span class="hljs-string">&quot;sum(amount) &gt; ?&quot;</span>, <span class="hljs-number">100</span>).Scan(&amp;results)
</code></pre> </code></pre>
<h3 id="joins">Joins</h3> <h3 id="joins">Joins</h3>
<pre><code class="lang-go">rows<span class="token operator">,</span> err <span class="token operator">:=</span> db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;users&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;users.name, emails.email&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Joins<span class="token punctuation">(</span></span><span class="token string">&quot;left join emails on emails.user_id = users.id&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Rows<span class="token punctuation">(</span></span><span class="token operator">)</span> <pre><code class="lang-go">rows, err := db.Table(<span class="hljs-string">&quot;users&quot;</span>).Select(<span class="hljs-string">&quot;users.name, emails.email&quot;</span>).Joins(<span class="hljs-string">&quot;left join emails on emails.user_id = users.id&quot;</span>).Rows()
<span class="token keyword">for</span> rows<span class="token operator">.</span><span class="token function">Next<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">for</span> rows.Next() {
<span class="token operator">...</span> ...
<span class="token operator">}</span> }
db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;users&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;users.name, emails.email&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Joins<span class="token punctuation">(</span></span><span class="token string">&quot;left join emails on emails.user_id = users.id&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Scan<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>results<span class="token operator">)</span> db.Table(<span class="hljs-string">&quot;users&quot;</span>).Select(<span class="hljs-string">&quot;users.name, emails.email&quot;</span>).Joins(<span class="hljs-string">&quot;left join emails on emails.user_id = users.id&quot;</span>).Scan(&amp;results)
<span class="token comment" spellcheck="true">// multiple joins with parameter</span> <span class="hljs-comment">// multiple joins with parameter</span>
db<span class="token operator">.</span><span class="token function">Joins<span class="token punctuation">(</span></span><span class="token string">&quot;JOIN emails ON emails.user_id = users.id AND emails.email = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu@example.org&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Joins<span class="token punctuation">(</span></span><span class="token string">&quot;JOIN credit_cards ON credit_cards.user_id = users.id&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;credit_cards.number = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;411111111111&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Joins(<span class="hljs-string">&quot;JOIN emails ON emails.user_id = users.id AND emails.email = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu@example.org&quot;</span>).Joins(<span class="hljs-string">&quot;JOIN credit_cards ON credit_cards.user_id = users.id&quot;</span>).Where(<span class="hljs-string">&quot;credit_cards.number = ?&quot;</span>, <span class="hljs-string">&quot;411111111111&quot;</span>).Find(&amp;user)
</code></pre> </code></pre>
<h3 id="pluck">Pluck</h3> <h3 id="pluck">Pluck</h3>
<p>Get selected attributes as map</p> <p>Get selected attributes as map</p>
<pre><code class="lang-go"><span class="token keyword">var</span> ages <span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">int64</span> <pre><code class="lang-go"><span class="hljs-keyword">var</span> ages []<span class="hljs-typename">int64</span>
db<span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Pluck<span class="token punctuation">(</span></span><span class="token string">&quot;age&quot;</span><span class="token operator">,</span> <span class="token operator">&amp;</span>ages<span class="token operator">)</span> db.Find(&amp;users).Pluck(<span class="hljs-string">&quot;age&quot;</span>, &amp;ages)
<span class="token keyword">var</span> names <span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">string</span> <span class="hljs-keyword">var</span> names []<span class="hljs-typename">string</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Pluck<span class="token punctuation">(</span></span><span class="token string">&quot;name&quot;</span><span class="token operator">,</span> <span class="token operator">&amp;</span>names<span class="token operator">)</span> db.Model(&amp;User{}).Pluck(<span class="hljs-string">&quot;name&quot;</span>, &amp;names)
db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;deleted_users&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Pluck<span class="token punctuation">(</span></span><span class="token string">&quot;name&quot;</span><span class="token operator">,</span> <span class="token operator">&amp;</span>names<span class="token operator">)</span> db.Table(<span class="hljs-string">&quot;deleted_users&quot;</span>).Pluck(<span class="hljs-string">&quot;name&quot;</span>, &amp;names)
<span class="token comment" spellcheck="true">// Requesting more than one column? Do it like this:</span> <span class="hljs-comment">// Requesting more than one column? Do it like this:</span>
db<span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;name, age&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Select(<span class="hljs-string">&quot;name, age&quot;</span>).Find(&amp;users)
</code></pre> </code></pre>
<h3 id="scan">Scan</h3> <h3 id="scan">Scan</h3>
<p>Scan results into another struct.</p> <p>Scan results into another struct.</p>
<pre><code class="lang-go"><span class="token keyword">type</span> Result <span class="token keyword">struct</span> <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">type</span> Result <span class="hljs-keyword">struct</span> {
Name <span class="token builtin">string</span> Name <span class="hljs-typename">string</span>
Age <span class="token builtin">int</span> Age <span class="hljs-typename">int</span>
<span class="token operator">}</span> }
<span class="token keyword">var</span> result Result <span class="hljs-keyword">var</span> result Result
db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;users&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;name, age&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token number">3</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Scan<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>result<span class="token operator">)</span> db.Table(<span class="hljs-string">&quot;users&quot;</span>).Select(<span class="hljs-string">&quot;name, age&quot;</span>).Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-number">3</span>).Scan(&amp;result)
<span class="token comment" spellcheck="true">// Raw SQL</span> <span class="hljs-comment">// Raw SQL</span>
db<span class="token operator">.</span><span class="token function">Raw<span class="token punctuation">(</span></span><span class="token string">&quot;SELECT name, age FROM users WHERE name = ?&quot;</span><span class="token operator">,</span> <span class="token number">3</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Scan<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>result<span class="token operator">)</span> db.Raw(<span class="hljs-string">&quot;SELECT name, age FROM users WHERE name = ?&quot;</span>, <span class="hljs-number">3</span>).Scan(&amp;result)
</code></pre> </code></pre>
<h3 id="scopes">Scopes</h3> <h3 id="scopes">Scopes</h3>
<pre><code class="lang-go"><span class="token keyword">func</span> <span class="token function">AmountGreaterThan1000<span class="token punctuation">(</span></span>db <span class="token operator">*</span>gorm<span class="token operator">.</span>DB<span class="token operator">)</span> <span class="token operator">*</span>gorm<span class="token operator">.</span>DB <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">func</span> AmountGreaterThan1000(db *gorm.DB) *gorm.DB {
<span class="token keyword">return</span> db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;amount &gt; ?&quot;</span><span class="token operator">,</span> <span class="token number">1000</span><span class="token operator">)</span> <span class="hljs-keyword">return</span> db.Where(<span class="hljs-string">&quot;amount &gt; ?&quot;</span>, <span class="hljs-number">1000</span>)
<span class="token operator">}</span> }
<span class="token keyword">func</span> <span class="token function">PaidWithCreditCard<span class="token punctuation">(</span></span>db <span class="token operator">*</span>gorm<span class="token operator">.</span>DB<span class="token operator">)</span> <span class="token operator">*</span>gorm<span class="token operator">.</span>DB <span class="token operator">{</span> <span class="hljs-keyword">func</span> PaidWithCreditCard(db *gorm.DB) *gorm.DB {
<span class="token keyword">return</span> db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;pay_mode_sign = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;C&quot;</span><span class="token operator">)</span> <span class="hljs-keyword">return</span> db.Where(<span class="hljs-string">&quot;pay_mode_sign = ?&quot;</span>, <span class="hljs-string">&quot;C&quot;</span>)
<span class="token operator">}</span> }
<span class="token keyword">func</span> <span class="token function">PaidWithCod<span class="token punctuation">(</span></span>db <span class="token operator">*</span>gorm<span class="token operator">.</span>DB<span class="token operator">)</span> <span class="token operator">*</span>gorm<span class="token operator">.</span>DB <span class="token operator">{</span> <span class="hljs-keyword">func</span> PaidWithCod(db *gorm.DB) *gorm.DB {
<span class="token keyword">return</span> db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;pay_mode_sign = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;C&quot;</span><span class="token operator">)</span> <span class="hljs-keyword">return</span> db.Where(<span class="hljs-string">&quot;pay_mode_sign = ?&quot;</span>, <span class="hljs-string">&quot;C&quot;</span>)
<span class="token operator">}</span> }
<span class="token keyword">func</span> <span class="token function">OrderStatus<span class="token punctuation">(</span></span>status <span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">string</span><span class="token operator">)</span> <span class="token keyword">func</span> <span class="token operator">(</span>db <span class="token operator">*</span>gorm<span class="token operator">.</span>DB<span class="token operator">)</span> <span class="token operator">*</span>gorm<span class="token operator">.</span>DB <span class="token operator">{</span> <span class="hljs-keyword">func</span> OrderStatus(status []<span class="hljs-typename">string</span>) <span class="hljs-keyword">func</span> (db *gorm.DB) *gorm.DB {
<span class="token keyword">return</span> <span class="token keyword">func</span> <span class="token operator">(</span>db <span class="token operator">*</span>gorm<span class="token operator">.</span>DB<span class="token operator">)</span> <span class="token operator">*</span>gorm<span class="token operator">.</span>DB <span class="token operator">{</span> <span class="hljs-keyword">return</span> <span class="hljs-keyword">func</span> (db *gorm.DB) *gorm.DB {
<span class="token keyword">return</span> db<span class="token operator">.</span><span class="token function">Scopes<span class="token punctuation">(</span></span>AmountGreaterThan1000<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;status in (?)&quot;</span><span class="token operator">,</span> status<span class="token operator">)</span> <span class="hljs-keyword">return</span> db.Scopes(AmountGreaterThan1000).Where(<span class="hljs-string">&quot;status in (?)&quot;</span>, status)
<span class="token operator">}</span> }
<span class="token operator">}</span> }
db<span class="token operator">.</span><span class="token function">Scopes<span class="token punctuation">(</span></span>AmountGreaterThan1000<span class="token operator">,</span> PaidWithCreditCard<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>orders<span class="token operator">)</span> db.Scopes(AmountGreaterThan1000, PaidWithCreditCard).Find(&amp;orders)
<span class="token comment" spellcheck="true">// Find all credit card orders and amount greater than 1000</span> <span class="hljs-comment">// Find all credit card orders and amount greater than 1000</span>
db<span class="token operator">.</span><span class="token function">Scopes<span class="token punctuation">(</span></span>AmountGreaterThan1000<span class="token operator">,</span> PaidWithCod<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>orders<span class="token operator">)</span> db.Scopes(AmountGreaterThan1000, PaidWithCod).Find(&amp;orders)
<span class="token comment" spellcheck="true">// Find all COD orders and amount greater than 1000</span> <span class="hljs-comment">// Find all COD orders and amount greater than 1000</span>
db<span class="token operator">.</span><span class="token function">Scopes<span class="token punctuation">(</span></span><span class="token function">OrderStatus<span class="token punctuation">(</span></span><span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">string</span><span class="token operator">{</span><span class="token string">&quot;paid&quot;</span><span class="token operator">,</span> <span class="token string">&quot;shipped&quot;</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>orders<span class="token operator">)</span> db.Scopes(OrderStatus([]<span class="hljs-typename">string</span>{<span class="hljs-string">&quot;paid&quot;</span>, <span class="hljs-string">&quot;shipped&quot;</span>})).Find(&amp;orders)
<span class="token comment" spellcheck="true">// Find all paid, shipped orders</span> <span class="hljs-comment">// Find all paid, shipped orders</span>
</code></pre> </code></pre>
<h3 id="specifying-the-table-name">Specifying The Table Name</h3> <h3 id="specifying-the-table-name">Specifying The Table Name</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Create `deleted_users` table with struct User&apos;s definition</span> <pre><code class="lang-go"><span class="hljs-comment">// Create `deleted_users` table with struct User&apos;s definition</span>
db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;deleted_users&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">CreateTable<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span> db.Table(<span class="hljs-string">&quot;deleted_users&quot;</span>).CreateTable(&amp;User{})
<span class="token keyword">var</span> deleted_users <span class="token operator">[</span><span class="token operator">]</span>User <span class="hljs-keyword">var</span> deleted_users []User
db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;deleted_users&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>deleted_users<span class="token operator">)</span> db.Table(<span class="hljs-string">&quot;deleted_users&quot;</span>).Find(&amp;deleted_users)
<span class="token comment" spellcheck="true">//// SELECT * FROM deleted_users;</span> <span class="hljs-comment">//// SELECT * FROM deleted_users;</span>
db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;deleted_users&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;name = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Delete<span class="token punctuation">(</span></span><span class="token operator">)</span> db.Table(<span class="hljs-string">&quot;deleted_users&quot;</span>).Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>).Delete()
<span class="token comment" spellcheck="true">//// DELETE FROM deleted_users WHERE name = &apos;jinzhu&apos;;</span> <span class="hljs-comment">//// DELETE FROM deleted_users WHERE name = &apos;jinzhu&apos;;</span>
</code></pre> </code></pre>
<h2 id="update">Update</h2> <h2 id="update">Update</h2>
<h3 id="update-all-fields">Update All Fields</h3> <h3 id="update-all-fields">Update All Fields</h3>
<p><code>Save</code> will include all fields when perform the Updating SQL, even it is not changed</p> <p><code>Save</code> will include all fields when perform the Updating SQL, even it is not changed</p>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> <pre><code class="lang-go">db.First(&amp;user)
user<span class="token operator">.</span>Name <span class="token operator">=</span> <span class="token string">&quot;jinzhu 2&quot;</span> user.Name = <span class="hljs-string">&quot;jinzhu 2&quot;</span>
user<span class="token operator">.</span>Age <span class="token operator">=</span> <span class="token number">100</span> user.Age = <span class="hljs-number">100</span>
db<span class="token operator">.</span><span class="token function">Save<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Save(&amp;user)
<span class="token comment" spellcheck="true">//// UPDATE users SET name=&apos;jinzhu 2&apos;, age=100, birthday=&apos;2016-01-01&apos;, updated_at = &apos;2013-11-17 21:34:10&apos; WHERE id=111;</span> <span class="hljs-comment">//// UPDATE users SET name=&apos;jinzhu 2&apos;, age=100, birthday=&apos;2016-01-01&apos;, updated_at = &apos;2013-11-17 21:34:10&apos; WHERE id=111;</span>
</code></pre> </code></pre>
<h3 id="update-changed-fields">Update Changed Fields</h3> <h3 id="update-changed-fields">Update Changed Fields</h3>
<p>If you only want to update changed Fields, you could use <code>Update</code>, <code>Updates</code></p> <p>If you only want to update changed Fields, you could use <code>Update</code>, <code>Updates</code></p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Update single attribute if it is changed</span> <pre><code class="lang-go"><span class="hljs-comment">// Update single attribute if it is changed</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Update<span class="token punctuation">(</span></span><span class="token string">&quot;name&quot;</span><span class="token operator">,</span> <span class="token string">&quot;hello&quot;</span><span class="token operator">)</span> db.Model(&amp;user).Update(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;hello&quot;</span>)
<span class="token comment" spellcheck="true">//// UPDATE users SET name=&apos;hello&apos;, updated_at=&apos;2013-11-17 21:34:10&apos; WHERE id=111;</span> <span class="hljs-comment">//// UPDATE users SET name=&apos;hello&apos;, updated_at=&apos;2013-11-17 21:34:10&apos; WHERE id=111;</span>
<span class="token comment" spellcheck="true">// Update single attribute with combined conditions</span> <span class="hljs-comment">// Update single attribute with combined conditions</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;active = ?&quot;</span><span class="token operator">,</span> <span class="token boolean">true</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Update<span class="token punctuation">(</span></span><span class="token string">&quot;name&quot;</span><span class="token operator">,</span> <span class="token string">&quot;hello&quot;</span><span class="token operator">)</span> db.Model(&amp;user).Where(<span class="hljs-string">&quot;active = ?&quot;</span>, <span class="hljs-constant">true</span>).Update(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;hello&quot;</span>)
<span class="token comment" spellcheck="true">//// UPDATE users SET name=&apos;hello&apos;, updated_at=&apos;2013-11-17 21:34:10&apos; WHERE id=111 AND active=true;</span> <span class="hljs-comment">//// UPDATE users SET name=&apos;hello&apos;, updated_at=&apos;2013-11-17 21:34:10&apos; WHERE id=111 AND active=true;</span>
<span class="token comment" spellcheck="true">// Update multiple attributes with `map`, will only update those changed fields</span> <span class="hljs-comment">// Update multiple attributes with `map`, will only update those changed fields</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Updates<span class="token punctuation">(</span></span><span class="token keyword">map</span><span class="token operator">[</span><span class="token builtin">string</span><span class="token operator">]</span><span class="token keyword">interface</span><span class="token operator">{</span><span class="token operator">}</span><span class="token operator">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;hello&quot;</span><span class="token operator">,</span> <span class="token string">&quot;age&quot;</span><span class="token operator">:</span> <span class="token number">18</span><span class="token operator">,</span> <span class="token string">&quot;actived&quot;</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token operator">}</span><span class="token operator">)</span> db.Model(&amp;user).Updates(<span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]<span class="hljs-keyword">interface</span>{}{<span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-string">&quot;age&quot;</span>: <span class="hljs-number">18</span>, <span class="hljs-string">&quot;actived&quot;</span>: <span class="hljs-constant">false</span>})
<span class="token comment" spellcheck="true">//// UPDATE users SET name=&apos;hello&apos;, age=18, actived=false, updated_at=&apos;2013-11-17 21:34:10&apos; WHERE id=111;</span> <span class="hljs-comment">//// UPDATE users SET name=&apos;hello&apos;, age=18, actived=false, updated_at=&apos;2013-11-17 21:34:10&apos; WHERE id=111;</span>
<span class="token comment" spellcheck="true">// Update multiple attributes with `struct`, will only update those changed &amp; non blank fields</span> <span class="hljs-comment">// Update multiple attributes with `struct`, will only update those changed &amp; non blank fields</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Updates<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;hello&quot;</span><span class="token operator">,</span> Age<span class="token operator">:</span> <span class="token number">18</span><span class="token operator">}</span><span class="token operator">)</span> db.Model(&amp;user).Updates(User{Name: <span class="hljs-string">&quot;hello&quot;</span>, Age: <span class="hljs-number">18</span>})
<span class="token comment" spellcheck="true">//// UPDATE users SET name=&apos;hello&apos;, age=18, updated_at = &apos;2013-11-17 21:34:10&apos; WHERE id = 111;</span> <span class="hljs-comment">//// UPDATE users SET name=&apos;hello&apos;, age=18, updated_at = &apos;2013-11-17 21:34:10&apos; WHERE id = 111;</span>
<span class="token comment" spellcheck="true">// WARNING when update with struct, GORM will only update those fields that with non blank value</span> <span class="hljs-comment">// WARNING when update with struct, GORM will only update those fields that with non blank value</span>
<span class="token comment" spellcheck="true">// For below Update, nothing will be updated as &quot;&quot;, 0, false are blank values of their types</span> <span class="hljs-comment">// For below Update, nothing will be updated as &quot;&quot;, 0, false are blank values of their types</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Updates<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token operator">,</span> Age<span class="token operator">:</span> <span class="token number">0</span><span class="token operator">,</span> Actived<span class="token operator">:</span> <span class="token boolean">false</span><span class="token operator">}</span><span class="token operator">)</span> db.Model(&amp;user).Updates(User{Name: <span class="hljs-string">&quot;&quot;</span>, Age: <span class="hljs-number">0</span>, Actived: <span class="hljs-constant">false</span>})
</code></pre> </code></pre>
<h3 id="update-selected-fields">Update Selected Fields</h3> <h3 id="update-selected-fields">Update Selected Fields</h3>
<p>If you only want to update or ignore some fields when updating, you could use <code>Select</code>, <code>Omit</code></p> <p>If you only want to update or ignore some fields when updating, you could use <code>Select</code>, <code>Omit</code></p>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Select<span class="token punctuation">(</span></span><span class="token string">&quot;name&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Updates<span class="token punctuation">(</span></span><span class="token keyword">map</span><span class="token operator">[</span><span class="token builtin">string</span><span class="token operator">]</span><span class="token keyword">interface</span><span class="token operator">{</span><span class="token operator">}</span><span class="token operator">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;hello&quot;</span><span class="token operator">,</span> <span class="token string">&quot;age&quot;</span><span class="token operator">:</span> <span class="token number">18</span><span class="token operator">,</span> <span class="token string">&quot;actived&quot;</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token operator">}</span><span class="token operator">)</span> <pre><code class="lang-go">db.Model(&amp;user).Select(<span class="hljs-string">&quot;name&quot;</span>).Updates(<span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]<span class="hljs-keyword">interface</span>{}{<span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-string">&quot;age&quot;</span>: <span class="hljs-number">18</span>, <span class="hljs-string">&quot;actived&quot;</span>: <span class="hljs-constant">false</span>})
<span class="token comment" spellcheck="true">//// UPDATE users SET name=&apos;hello&apos;, updated_at=&apos;2013-11-17 21:34:10&apos; WHERE id=111;</span> <span class="hljs-comment">//// UPDATE users SET name=&apos;hello&apos;, updated_at=&apos;2013-11-17 21:34:10&apos; WHERE id=111;</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Omit<span class="token punctuation">(</span></span><span class="token string">&quot;name&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Updates<span class="token punctuation">(</span></span><span class="token keyword">map</span><span class="token operator">[</span><span class="token builtin">string</span><span class="token operator">]</span><span class="token keyword">interface</span><span class="token operator">{</span><span class="token operator">}</span><span class="token operator">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;hello&quot;</span><span class="token operator">,</span> <span class="token string">&quot;age&quot;</span><span class="token operator">:</span> <span class="token number">18</span><span class="token operator">,</span> <span class="token string">&quot;actived&quot;</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token operator">}</span><span class="token operator">)</span> db.Model(&amp;user).Omit(<span class="hljs-string">&quot;name&quot;</span>).Updates(<span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]<span class="hljs-keyword">interface</span>{}{<span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-string">&quot;age&quot;</span>: <span class="hljs-number">18</span>, <span class="hljs-string">&quot;actived&quot;</span>: <span class="hljs-constant">false</span>})
<span class="token comment" spellcheck="true">//// UPDATE users SET age=18, actived=false, updated_at=&apos;2013-11-17 21:34:10&apos; WHERE id=111;</span> <span class="hljs-comment">//// UPDATE users SET age=18, actived=false, updated_at=&apos;2013-11-17 21:34:10&apos; WHERE id=111;</span>
</code></pre> </code></pre>
<h3 id="update-changed-fields-without-callbacks">Update Changed Fields Without Callbacks</h3> <h3 id="update-changed-fields-without-callbacks">Update Changed Fields Without Callbacks</h3>
<p>Updating operations above will invoke <code>BeforeUpdate</code>, <code>AfterUpdate</code>, Update UpdatedAt timestamp, Save Associations callbacks, if you don&apos;t call them, you could use <code>UpdateColumn</code>, <code>UpdateColumns</code></p> <p>Updating operations above will invoke <code>BeforeUpdate</code>, <code>AfterUpdate</code>, Update UpdatedAt timestamp, Save Associations callbacks, if you don&apos;t call them, you could use <code>UpdateColumn</code>, <code>UpdateColumns</code></p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Update single attribute, similar with `Update`</span> <pre><code class="lang-go"><span class="hljs-comment">// Update single attribute, similar with `Update`</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">UpdateColumn<span class="token punctuation">(</span></span><span class="token string">&quot;name&quot;</span><span class="token operator">,</span> <span class="token string">&quot;hello&quot;</span><span class="token operator">)</span> db.Model(&amp;user).UpdateColumn(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;hello&quot;</span>)
<span class="token comment" spellcheck="true">//// UPDATE users SET name=&apos;hello&apos; WHERE id = 111;</span> <span class="hljs-comment">//// UPDATE users SET name=&apos;hello&apos; WHERE id = 111;</span>
<span class="token comment" spellcheck="true">// Update multiple attributes, similar with `Updates`</span> <span class="hljs-comment">// Update multiple attributes, similar with `Updates`</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">UpdateColumns<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;hello&quot;</span><span class="token operator">,</span> Age<span class="token operator">:</span> <span class="token number">18</span><span class="token operator">}</span><span class="token operator">)</span> db.Model(&amp;user).UpdateColumns(User{Name: <span class="hljs-string">&quot;hello&quot;</span>, Age: <span class="hljs-number">18</span>})
<span class="token comment" spellcheck="true">//// UPDATE users SET name=&apos;hello&apos;, age=18 WHERE id = 111;</span> <span class="hljs-comment">//// UPDATE users SET name=&apos;hello&apos;, age=18 WHERE id = 111;</span>
</code></pre> </code></pre>
<h3 id="batch-updates">Batch Updates</h3> <h3 id="batch-updates">Batch Updates</h3>
<p>Callbacks won&apos;t run when do batch updates</p> <p>Callbacks won&apos;t run when do batch updates</p>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Table<span class="token punctuation">(</span></span><span class="token string">&quot;users&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;id IN (?)&quot;</span><span class="token operator">,</span> <span class="token operator">[</span><span class="token operator">]</span><span class="token builtin">int</span><span class="token operator">{</span><span class="token number">10</span><span class="token operator">,</span> <span class="token number">11</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Updates<span class="token punctuation">(</span></span><span class="token keyword">map</span><span class="token operator">[</span><span class="token builtin">string</span><span class="token operator">]</span><span class="token keyword">interface</span><span class="token operator">{</span><span class="token operator">}</span><span class="token operator">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;hello&quot;</span><span class="token operator">,</span> <span class="token string">&quot;age&quot;</span><span class="token operator">:</span> <span class="token number">18</span><span class="token operator">}</span><span class="token operator">)</span> <pre><code class="lang-go">db.Table(<span class="hljs-string">&quot;users&quot;</span>).Where(<span class="hljs-string">&quot;id IN (?)&quot;</span>, []<span class="hljs-typename">int</span>{<span class="hljs-number">10</span>, <span class="hljs-number">11</span>}).Updates(<span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]<span class="hljs-keyword">interface</span>{}{<span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-string">&quot;age&quot;</span>: <span class="hljs-number">18</span>})
<span class="token comment" spellcheck="true">//// UPDATE users SET name=&apos;hello&apos;, age=18 WHERE id IN (10, 11);</span> <span class="hljs-comment">//// UPDATE users SET name=&apos;hello&apos;, age=18 WHERE id IN (10, 11);</span>
<span class="token comment" spellcheck="true">// Update with struct only works with none zero values, or use map[string]interface{}</span> <span class="hljs-comment">// Update with struct only works with none zero values, or use map[string]interface{}</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Updates<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;hello&quot;</span><span class="token operator">,</span> Age<span class="token operator">:</span> <span class="token number">18</span><span class="token operator">}</span><span class="token operator">)</span> db.Model(User{}).Updates(User{Name: <span class="hljs-string">&quot;hello&quot;</span>, Age: <span class="hljs-number">18</span>})
<span class="token comment" spellcheck="true">//// UPDATE users SET name=&apos;hello&apos;, age=18;</span> <span class="hljs-comment">//// UPDATE users SET name=&apos;hello&apos;, age=18;</span>
<span class="token comment" spellcheck="true">// Get updated records count with `RowsAffected`</span> <span class="hljs-comment">// Get updated records count with `RowsAffected`</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Updates<span class="token punctuation">(</span></span>User<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">&quot;hello&quot;</span><span class="token operator">,</span> Age<span class="token operator">:</span> <span class="token number">18</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span>RowsAffected db.Model(User{}).Updates(User{Name: <span class="hljs-string">&quot;hello&quot;</span>, Age: <span class="hljs-number">18</span>}).RowsAffected
</code></pre> </code></pre>
<h3 id="update-with-sql-expression">Update with SQL Expression</h3> <h3 id="update-with-sql-expression">Update with SQL Expression</h3>
<pre><code class="lang-go">DB<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>product<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Update<span class="token punctuation">(</span></span><span class="token string">&quot;price&quot;</span><span class="token operator">,</span> gorm<span class="token operator">.</span><span class="token function">Expr<span class="token punctuation">(</span></span><span class="token string">&quot;price * ? + ?&quot;</span><span class="token operator">,</span> <span class="token number">2</span><span class="token operator">,</span> <span class="token number">100</span><span class="token operator">)</span><span class="token operator">)</span> <pre><code class="lang-go">DB.Model(&amp;product).Update(<span class="hljs-string">&quot;price&quot;</span>, gorm.Expr(<span class="hljs-string">&quot;price * ? + ?&quot;</span>, <span class="hljs-number">2</span>, <span class="hljs-number">100</span>))
<span class="token comment" spellcheck="true">//// UPDATE &quot;products&quot; SET &quot;price&quot; = price * &apos;2&apos; + &apos;100&apos;, &quot;updated_at&quot; = &apos;2013-11-17 21:34:10&apos; WHERE &quot;id&quot; = &apos;2&apos;;</span> <span class="hljs-comment">//// UPDATE &quot;products&quot; SET &quot;price&quot; = price * &apos;2&apos; + &apos;100&apos;, &quot;updated_at&quot; = &apos;2013-11-17 21:34:10&apos; WHERE &quot;id&quot; = &apos;2&apos;;</span>
DB<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>product<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Updates<span class="token punctuation">(</span></span><span class="token keyword">map</span><span class="token operator">[</span><span class="token builtin">string</span><span class="token operator">]</span><span class="token keyword">interface</span><span class="token operator">{</span><span class="token operator">}</span><span class="token operator">{</span><span class="token string">&quot;price&quot;</span><span class="token operator">:</span> gorm<span class="token operator">.</span><span class="token function">Expr<span class="token punctuation">(</span></span><span class="token string">&quot;price * ? + ?&quot;</span><span class="token operator">,</span> <span class="token number">2</span><span class="token operator">,</span> <span class="token number">100</span><span class="token operator">)</span><span class="token operator">}</span><span class="token operator">)</span> DB.Model(&amp;product).Updates(<span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]<span class="hljs-keyword">interface</span>{}{<span class="hljs-string">&quot;price&quot;</span>: gorm.Expr(<span class="hljs-string">&quot;price * ? + ?&quot;</span>, <span class="hljs-number">2</span>, <span class="hljs-number">100</span>)})
<span class="token comment" spellcheck="true">//// UPDATE &quot;products&quot; SET &quot;price&quot; = price * &apos;2&apos; + &apos;100&apos;, &quot;updated_at&quot; = &apos;2013-11-17 21:34:10&apos; WHERE &quot;id&quot; = &apos;2&apos;;</span> <span class="hljs-comment">//// UPDATE &quot;products&quot; SET &quot;price&quot; = price * &apos;2&apos; + &apos;100&apos;, &quot;updated_at&quot; = &apos;2013-11-17 21:34:10&apos; WHERE &quot;id&quot; = &apos;2&apos;;</span>
DB<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>product<span class="token operator">)</span><span class="token operator">.</span><span class="token function">UpdateColumn<span class="token punctuation">(</span></span><span class="token string">&quot;quantity&quot;</span><span class="token operator">,</span> gorm<span class="token operator">.</span><span class="token function">Expr<span class="token punctuation">(</span></span><span class="token string">&quot;quantity - ?&quot;</span><span class="token operator">,</span> <span class="token number">1</span><span class="token operator">)</span><span class="token operator">)</span> DB.Model(&amp;product).UpdateColumn(<span class="hljs-string">&quot;quantity&quot;</span>, gorm.Expr(<span class="hljs-string">&quot;quantity - ?&quot;</span>, <span class="hljs-number">1</span>))
<span class="token comment" spellcheck="true">//// UPDATE &quot;products&quot; SET &quot;quantity&quot; = quantity - 1 WHERE &quot;id&quot; = &apos;2&apos;;</span> <span class="hljs-comment">//// UPDATE &quot;products&quot; SET &quot;quantity&quot; = quantity - 1 WHERE &quot;id&quot; = &apos;2&apos;;</span>
DB<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>product<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;quantity &gt; 1&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">UpdateColumn<span class="token punctuation">(</span></span><span class="token string">&quot;quantity&quot;</span><span class="token operator">,</span> gorm<span class="token operator">.</span><span class="token function">Expr<span class="token punctuation">(</span></span><span class="token string">&quot;quantity - ?&quot;</span><span class="token operator">,</span> <span class="token number">1</span><span class="token operator">)</span><span class="token operator">)</span> DB.Model(&amp;product).Where(<span class="hljs-string">&quot;quantity &gt; 1&quot;</span>).UpdateColumn(<span class="hljs-string">&quot;quantity&quot;</span>, gorm.Expr(<span class="hljs-string">&quot;quantity - ?&quot;</span>, <span class="hljs-number">1</span>))
<span class="token comment" spellcheck="true">//// UPDATE &quot;products&quot; SET &quot;quantity&quot; = quantity - 1 WHERE &quot;id&quot; = &apos;2&apos; AND quantity &gt; 1;</span> <span class="hljs-comment">//// UPDATE &quot;products&quot; SET &quot;quantity&quot; = quantity - 1 WHERE &quot;id&quot; = &apos;2&apos; AND quantity &gt; 1;</span>
</code></pre> </code></pre>
<h3 id="change-updating-values-in-callbacks">Change Updating Values In Callbacks</h3> <h3 id="change-updating-values-in-callbacks">Change Updating Values In Callbacks</h3>
<p>If you want to change updating values in callbacks using <code>BeforeUpdate</code>, <code>BeforeSave</code>, you could use <code>scope.SetColumn</code>, for example:</p> <p>If you want to change updating values in callbacks using <code>BeforeUpdate</code>, <code>BeforeSave</code>, you could use <code>scope.SetColumn</code>, for example:</p>
<pre><code class="lang-go"><span class="token keyword">func</span> <span class="token operator">(</span>user <span class="token operator">*</span>User<span class="token operator">)</span> <span class="token function">BeforeSave<span class="token punctuation">(</span></span>scope <span class="token operator">*</span>gorm<span class="token operator">.</span>Scope<span class="token operator">)</span> <span class="token operator">(</span>err <span class="token builtin">error</span><span class="token operator">)</span> <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">func</span> (user *User) BeforeSave(scope *gorm.Scope) (err error) {
<span class="token keyword">if</span> pw<span class="token operator">,</span> err <span class="token operator">:=</span> bcrypt<span class="token operator">.</span><span class="token function">GenerateFromPassword<span class="token punctuation">(</span></span>user<span class="token operator">.</span>Password<span class="token operator">,</span> <span class="token number">0</span><span class="token operator">)</span><span class="token operator">;</span> err <span class="token operator">==</span> <span class="token boolean">nil</span> <span class="token operator">{</span> <span class="hljs-keyword">if</span> pw, err := bcrypt.GenerateFromPassword(user.Password, <span class="hljs-number">0</span>); err == <span class="hljs-constant">nil</span> {
scope<span class="token operator">.</span><span class="token function">SetColumn<span class="token punctuation">(</span></span><span class="token string">&quot;EncryptedPassword&quot;</span><span class="token operator">,</span> pw<span class="token operator">)</span> scope.SetColumn(<span class="hljs-string">&quot;EncryptedPassword&quot;</span>, pw)
<span class="token operator">}</span> }
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h3 id="extra-updating-option">Extra Updating option</h3> <h3 id="extra-updating-option">Extra Updating option</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Add extra SQL option for updating SQL</span> <pre><code class="lang-go">// Add extra SQL option for updating SQL
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Set<span class="token punctuation">(</span></span><span class="token string">&quot;gorm:update_option&quot;</span><span class="token operator">,</span> <span class="token string">&quot;OPTION (OPTIMIZE FOR UNKNOWN)&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Update<span class="token punctuation">(</span></span><span class="token string">&quot;name, &quot;</span>hello&quot;<span class="token operator">)</span> db.Model(&amp;user).Set(&quot;gorm:update_option&quot;, &quot;OPTION (OPTIMIZE FOR UNKNOWN)&quot;).Update(&quot;name, &quot;hello&quot;)
<span class="token comment" spellcheck="true">//// UPDATE users SET name=&apos;hello&apos;, updated_at = &apos;2013-11-17 21:34:10&apos; WHERE id=111 OPTION (OPTIMIZE FOR UNKNOWN);</span> //// UPDATE users SET name=&apos;hello&apos;, updated_at = &apos;2013-11-17 21:34:10&apos; WHERE id=111 OPTION (OPTIMIZE FOR UNKNOWN);
</code></pre> </code></pre>
<h2 id="delete">Delete</h2> <h2 id="delete">Delete</h2>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Delete an existing record</span> <pre><code class="lang-go"><span class="hljs-comment">// Delete an existing record</span>
db<span class="token operator">.</span><span class="token function">Delete<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>email<span class="token operator">)</span> db.Delete(&amp;email)
<span class="token comment" spellcheck="true">//// DELETE from emails where id=10;</span> <span class="hljs-comment">//// DELETE from emails where id=10;</span>
<span class="token comment" spellcheck="true">// Add extra SQL option for deleting SQL</span> <span class="hljs-comment">// Add extra SQL option for deleting SQL</span>
db<span class="token operator">.</span><span class="token function">Set<span class="token punctuation">(</span></span><span class="token string">&quot;gorm:delete_option&quot;</span><span class="token operator">,</span> <span class="token string">&quot;OPTION (OPTIMIZE FOR UNKNOWN)&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Delete<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>email<span class="token operator">)</span> db.Set(<span class="hljs-string">&quot;gorm:delete_option&quot;</span>, <span class="hljs-string">&quot;OPTION (OPTIMIZE FOR UNKNOWN)&quot;</span>).Delete(&amp;email)
<span class="token comment" spellcheck="true">//// DELETE from emails where id=10 OPTION (OPTIMIZE FOR UNKNOWN);</span> <span class="hljs-comment">//// DELETE from emails where id=10 OPTION (OPTIMIZE FOR UNKNOWN);</span>
</code></pre> </code></pre>
<h3 id="batch-delete">Batch Delete</h3> <h3 id="batch-delete">Batch Delete</h3>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;email LIKE ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;%jinzhu%&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Delete<span class="token punctuation">(</span></span>Email<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span> <pre><code class="lang-go">db.Where(<span class="hljs-string">&quot;email LIKE ?&quot;</span>, <span class="hljs-string">&quot;%jinzhu%&quot;</span>).Delete(Email{})
<span class="token comment" spellcheck="true">//// DELETE from emails where email LIKE &quot;%jinhu%&quot;;</span> <span class="hljs-comment">//// DELETE from emails where email LIKE &quot;%jinhu%&quot;;</span>
</code></pre> </code></pre>
<h3 id="soft-delete">Soft Delete</h3> <h3 id="soft-delete">Soft Delete</h3>
<p>If struct has <code>DeletedAt</code> field, it will get soft delete ability automatically! <p>If struct has <code>DeletedAt</code> field, it will get soft delete ability automatically!
Then it won&apos;t be deleted from database permanently when call <code>Delete</code>.</p> Then it won&apos;t be deleted from database permanently when call <code>Delete</code>.</p>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Delete<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> <pre><code class="lang-go">db.Delete(&amp;user)
<span class="token comment" spellcheck="true">//// UPDATE users SET deleted_at=&quot;2013-10-29 10:23&quot; WHERE id = 111;</span> <span class="hljs-comment">//// UPDATE users SET deleted_at=&quot;2013-10-29 10:23&quot; WHERE id = 111;</span>
<span class="token comment" spellcheck="true">// Batch Delete</span> <span class="hljs-comment">// Batch Delete</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;age = ?&quot;</span><span class="token operator">,</span> <span class="token number">20</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Delete<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;age = ?&quot;</span>, <span class="hljs-number">20</span>).Delete(&amp;User{})
<span class="token comment" spellcheck="true">//// UPDATE users SET deleted_at=&quot;2013-10-29 10:23&quot; WHERE age = 20;</span> <span class="hljs-comment">//// UPDATE users SET deleted_at=&quot;2013-10-29 10:23&quot; WHERE age = 20;</span>
<span class="token comment" spellcheck="true">// Soft deleted records will be ignored when query them</span> <span class="hljs-comment">// Soft deleted records will be ignored when query them</span>
db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;age = 20&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> db.Where(<span class="hljs-string">&quot;age = 20&quot;</span>).Find(&amp;user)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE age = 20 AND deleted_at IS NULL;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE age = 20 AND deleted_at IS NULL;</span>
<span class="token comment" spellcheck="true">// Find soft deleted records with Unscoped</span> <span class="hljs-comment">// Find soft deleted records with Unscoped</span>
db<span class="token operator">.</span><span class="token function">Unscoped<span class="token punctuation">(</span></span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">&quot;age = 20&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Find<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>users<span class="token operator">)</span> db.Unscoped().Where(<span class="hljs-string">&quot;age = 20&quot;</span>).Find(&amp;users)
<span class="token comment" spellcheck="true">//// SELECT * FROM users WHERE age = 20;</span> <span class="hljs-comment">//// SELECT * FROM users WHERE age = 20;</span>
<span class="token comment" spellcheck="true">// Delete record permanently with Unscoped</span> <span class="hljs-comment">// Delete record permanently with Unscoped</span>
db<span class="token operator">.</span><span class="token function">Unscoped<span class="token punctuation">(</span></span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Delete<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>order<span class="token operator">)</span> db.Unscoped().Delete(&amp;order)
<span class="token comment" spellcheck="true">//// DELETE FROM orders WHERE id=10;</span> <span class="hljs-comment">//// DELETE FROM orders WHERE id=10;</span>
</code></pre> </code></pre>
@ -1225,7 +1225,7 @@ db<span class="token operator">.</span><span class="token function">Unscoped<spa
<script> <script>
require(["gitbook"], function(gitbook) { require(["gitbook"], function(gitbook) {
gitbook.start({"fontsettings":{"theme":"night","family":"sans","size":4},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"prism":{},"anker-enable":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}}); gitbook.start({"fontsettings":{"theme":"night","size":2,"family":"sans"},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"anker-enable":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}});
}); });
</script> </script>

View File

@ -16,7 +16,7 @@
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css"> <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
@ -70,7 +70,7 @@
data-chapter-title="Database" data-chapter-title="Database"
data-filepath="database.md" data-filepath="database.md"
data-basepath="" data-basepath=""
data-revision="Mon Feb 29 2016 21:35:06 GMT+0800 (CST)" data-revision="Mon Feb 29 2016 21:51:55 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -607,46 +607,46 @@
<h2 id="connecting-to-a-database">Connecting to a database</h2> <h2 id="connecting-to-a-database">Connecting to a database</h2>
<h4 id="mysql">MySQL</h4> <h4 id="mysql">MySQL</h4>
<p><strong>NOTE</strong> don&apos;t forgot params <code>parseTime</code> to handle data type <code>time.Time</code>, <a href="https://github.com/go-sql-driver/mysql#parameters" target="_blank">more support parameters</a></p> <p><strong>NOTE</strong> don&apos;t forgot params <code>parseTime</code> to handle data type <code>time.Time</code>, <a href="https://github.com/go-sql-driver/mysql#parameters" target="_blank">more support parameters</a></p>
<pre><code class="lang-go"><span class="token keyword">import</span> <span class="token operator">(</span> <pre><code class="lang-go"><span class="hljs-keyword">import</span> (
<span class="token string">&quot;github.com/jinzhu/gorm&quot;</span> <span class="hljs-string">&quot;github.com/jinzhu/gorm&quot;</span>
<span class="token boolean">_</span> <span class="token string">&quot;github.com/go-sql-driver/mysql&quot;</span> _ <span class="hljs-string">&quot;github.com/go-sql-driver/mysql&quot;</span>
<span class="token operator">)</span> )
<span class="token keyword">func</span> <span class="token function">main<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">func</span> main() {
db<span class="token operator">,</span> err <span class="token operator">:=</span> gorm<span class="token operator">.</span><span class="token function">Open<span class="token punctuation">(</span></span><span class="token string">&quot;mysql&quot;</span><span class="token operator">,</span> <span class="token string">&quot;user:password@/dbname?charset=utf8&amp;parseTime=True&amp;loc=Local&quot;</span><span class="token operator">)</span> db, err := gorm.Open(<span class="hljs-string">&quot;mysql&quot;</span>, <span class="hljs-string">&quot;user:password@/dbname?charset=utf8&amp;parseTime=True&amp;loc=Local&quot;</span>)
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h4 id="postgresql">PostgreSQL</h4> <h4 id="postgresql">PostgreSQL</h4>
<pre><code class="lang-go"><span class="token keyword">import</span> <span class="token operator">(</span> <pre><code class="lang-go"><span class="hljs-keyword">import</span> (
<span class="token string">&quot;github.com/jinzhu/gorm&quot;</span> <span class="hljs-string">&quot;github.com/jinzhu/gorm&quot;</span>
<span class="token boolean">_</span> <span class="token string">&quot;github.com/lib/pq&quot;</span> _ <span class="hljs-string">&quot;github.com/lib/pq&quot;</span>
<span class="token operator">)</span> )
<span class="token keyword">func</span> <span class="token function">main<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">func</span> main() {
db<span class="token operator">,</span> err <span class="token operator">:=</span> gorm<span class="token operator">.</span><span class="token function">Open<span class="token punctuation">(</span></span><span class="token string">&quot;postgres&quot;</span><span class="token operator">,</span> <span class="token string">&quot;user=gorm dbname=gorm sslmode=disable&quot;</span><span class="token operator">)</span> db, err := gorm.Open(<span class="hljs-string">&quot;postgres&quot;</span>, <span class="hljs-string">&quot;user=gorm dbname=gorm sslmode=disable&quot;</span>)
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h4 id="sqlite3">Sqlite3</h4> <h4 id="sqlite3">Sqlite3</h4>
<pre><code class="lang-go"><span class="token keyword">import</span> <span class="token operator">(</span> <pre><code class="lang-go"><span class="hljs-keyword">import</span> (
<span class="token string">&quot;github.com/jinzhu/gorm&quot;</span> <span class="hljs-string">&quot;github.com/jinzhu/gorm&quot;</span>
<span class="token boolean">_</span> <span class="token string">&quot;github.com/mattn/go-sqlite3&quot;</span> _ <span class="hljs-string">&quot;github.com/mattn/go-sqlite3&quot;</span>
<span class="token operator">)</span> )
<span class="token keyword">func</span> <span class="token function">main<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">func</span> main() {
db<span class="token operator">,</span> err <span class="token operator">:=</span> gorm<span class="token operator">.</span><span class="token function">Open<span class="token punctuation">(</span></span><span class="token string">&quot;sqlite3&quot;</span><span class="token operator">,</span> <span class="token string">&quot;/tmp/gorm.db&quot;</span><span class="token operator">)</span> db, err := gorm.Open(<span class="hljs-string">&quot;sqlite3&quot;</span>, <span class="hljs-string">&quot;/tmp/gorm.db&quot;</span>)
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h4 id="write-dialect-for-unsupported-databases">Write Dialect for unsupported databases</h4> <h4 id="write-dialect-for-unsupported-databases">Write Dialect for unsupported databases</h4>
<p>GORM officially support above databases, for unsupported databaes, you could write a dialect for that.</p> <p>GORM officially support above databases, for unsupported databaes, you could write a dialect for that.</p>
<p>Refer: <a href="https://github.com/jinzhu/gorm/blob/master/dialect.go" target="_blank">https://github.com/jinzhu/gorm/blob/master/dialect.go</a></p> <p>Refer: <a href="https://github.com/jinzhu/gorm/blob/master/dialect.go" target="_blank">https://github.com/jinzhu/gorm/blob/master/dialect.go</a></p>
<h2 id="generic-database-object-sqldb">Generic database object *sql.DB</h2> <h2 id="generic-database-object-sqldb">Generic database object *sql.DB</h2>
<p><a href="http://golang.org/pkg/database/sql/#DB" target="_blank">*sql.DB</a></p> <p><a href="http://golang.org/pkg/database/sql/#DB" target="_blank">*sql.DB</a></p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Get generic database object *sql.DB to use its functions</span> <pre><code class="lang-go"><span class="hljs-comment">// Get generic database object *sql.DB to use its functions</span>
db<span class="token operator">.</span><span class="token function">DB<span class="token punctuation">(</span></span><span class="token operator">)</span> db.DB()
<span class="token comment" spellcheck="true">// Connection Pool</span> <span class="hljs-comment">// Connection Pool</span>
db<span class="token operator">.</span><span class="token function">DB<span class="token punctuation">(</span></span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">SetMaxIdleConns<span class="token punctuation">(</span></span><span class="token number">10</span><span class="token operator">)</span> db.DB().SetMaxIdleConns(<span class="hljs-number">10</span>)
db<span class="token operator">.</span><span class="token function">DB<span class="token punctuation">(</span></span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">SetMaxOpenConns<span class="token punctuation">(</span></span><span class="token number">100</span><span class="token operator">)</span> db.DB().SetMaxOpenConns(<span class="hljs-number">100</span>)
<span class="token comment" spellcheck="true">// Ping</span> <span class="hljs-comment">// Ping</span>
db<span class="token operator">.</span><span class="token function">DB<span class="token punctuation">(</span></span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">Ping<span class="token punctuation">(</span></span><span class="token operator">)</span> db.DB().Ping()
</code></pre> </code></pre>
<h2 id="migration">Migration</h2> <h2 id="migration">Migration</h2>
<!-- toc --> <!-- toc -->
@ -654,60 +654,60 @@ db<span class="token operator">.</span><span class="token function">DB<span clas
<p>Automatically migrate your schema, to keep your schema update to date</p> <p>Automatically migrate your schema, to keep your schema update to date</p>
<p><strong>WARNING</strong> AutoMigrate will ONLY create tables, columns and indexes if doesn&apos;t exist, <p><strong>WARNING</strong> AutoMigrate will ONLY create tables, columns and indexes if doesn&apos;t exist,
WON&apos;T change existing column&apos;s type or delete unused columns to protect your data</p> WON&apos;T change existing column&apos;s type or delete unused columns to protect your data</p>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">AutoMigrate<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span> <pre><code class="lang-go">db.AutoMigrate(&amp;User{})
db<span class="token operator">.</span><span class="token function">AutoMigrate<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">,</span> <span class="token operator">&amp;</span>Product<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">,</span> <span class="token operator">&amp;</span>Order<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span> db.AutoMigrate(&amp;User{}, &amp;Product{}, &amp;Order{})
<span class="token comment" spellcheck="true">// Add table suffix when create tables</span> <span class="hljs-comment">// Add table suffix when create tables</span>
db<span class="token operator">.</span><span class="token function">Set<span class="token punctuation">(</span></span><span class="token string">&quot;gorm:table_options&quot;</span><span class="token operator">,</span> <span class="token string">&quot;ENGINE=InnoDB&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">AutoMigrate<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span> db.Set(<span class="hljs-string">&quot;gorm:table_options&quot;</span>, <span class="hljs-string">&quot;ENGINE=InnoDB&quot;</span>).AutoMigrate(&amp;User{})
</code></pre> </code></pre>
<h3 id="has-table">Has Table</h3> <h3 id="has-table">Has Table</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Check if model `User`&apos;s table has been created or not</span> <pre><code class="lang-go"><span class="hljs-comment">// Check if model `User`&apos;s table has been created or not</span>
db<span class="token operator">.</span><span class="token function">HasTable<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span> db.HasTable(&amp;User{})
<span class="token comment" spellcheck="true">// Check table `users` exists or not</span> <span class="hljs-comment">// Check table `users` exists or not</span>
db<span class="token operator">.</span><span class="token function">HasTable<span class="token punctuation">(</span></span><span class="token string">&quot;users&quot;</span><span class="token operator">)</span> db.HasTable(<span class="hljs-string">&quot;users&quot;</span>)
</code></pre> </code></pre>
<h3 id="create-table">Create Table</h3> <h3 id="create-table">Create Table</h3>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">CreateTable<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span> <pre><code class="lang-go">db.CreateTable(&amp;User{})
db<span class="token operator">.</span><span class="token function">Set<span class="token punctuation">(</span></span><span class="token string">&quot;gorm:table_options&quot;</span><span class="token operator">,</span> <span class="token string">&quot;ENGINE=InnoDB&quot;</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">CreateTable<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span> db.Set(<span class="hljs-string">&quot;gorm:table_options&quot;</span>, <span class="hljs-string">&quot;ENGINE=InnoDB&quot;</span>).CreateTable(&amp;User{})
<span class="token comment" spellcheck="true">// will append &quot;ENGINE=InnoDB&quot; to the SQL statement when creating table `users`</span> <span class="hljs-comment">// will append &quot;ENGINE=InnoDB&quot; to the SQL statement when creating table `users`</span>
</code></pre> </code></pre>
<h3 id="drop-table">Drop table</h3> <h3 id="drop-table">Drop table</h3>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">DropTable<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span> <pre><code class="lang-go">db.DropTable(&amp;User{})
</code></pre> </code></pre>
<h3 id="modifycolumn">ModifyColumn</h3> <h3 id="modifycolumn">ModifyColumn</h3>
<p>Change column&apos;s type</p> <p>Change column&apos;s type</p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// change column description&apos;s data type to `text` for model `User`&apos;s table</span> <pre><code class="lang-go"><span class="hljs-comment">// change column description&apos;s data type to `text` for model `User`&apos;s table</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">ModifyColumn<span class="token punctuation">(</span></span><span class="token string">&quot;description&quot;</span><span class="token operator">,</span> <span class="token string">&quot;text&quot;</span><span class="token operator">)</span> db.Model(&amp;User{}).ModifyColumn(<span class="hljs-string">&quot;description&quot;</span>, <span class="hljs-string">&quot;text&quot;</span>)
</code></pre> </code></pre>
<h3 id="dropcolumn">DropColumn</h3> <h3 id="dropcolumn">DropColumn</h3>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">DropColumn<span class="token punctuation">(</span></span><span class="token string">&quot;description&quot;</span><span class="token operator">)</span> <pre><code class="lang-go">db.Model(&amp;User{}).DropColumn(<span class="hljs-string">&quot;description&quot;</span>)
</code></pre> </code></pre>
<h3 id="add-foreign-key">Add Foreign Key</h3> <h3 id="add-foreign-key">Add Foreign Key</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Add foreign key</span> <pre><code class="lang-go"><span class="hljs-comment">// Add foreign key</span>
<span class="token comment" spellcheck="true">// 1st param : foreignkey field</span> <span class="hljs-comment">// 1st param : foreignkey field</span>
<span class="token comment" spellcheck="true">// 2nd param : destination table(id)</span> <span class="hljs-comment">// 2nd param : destination table(id)</span>
<span class="token comment" spellcheck="true">// 3rd param : ONDELETE</span> <span class="hljs-comment">// 3rd param : ONDELETE</span>
<span class="token comment" spellcheck="true">// 4th param : ONUPDATE</span> <span class="hljs-comment">// 4th param : ONUPDATE</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">AddForeignKey<span class="token punctuation">(</span></span><span class="token string">&quot;city_id&quot;</span><span class="token operator">,</span> <span class="token string">&quot;cities(id)&quot;</span><span class="token operator">,</span> <span class="token string">&quot;RESTRICT&quot;</span><span class="token operator">,</span> <span class="token string">&quot;RESTRICT&quot;</span><span class="token operator">)</span> db.Model(&amp;User{}).AddForeignKey(<span class="hljs-string">&quot;city_id&quot;</span>, <span class="hljs-string">&quot;cities(id)&quot;</span>, <span class="hljs-string">&quot;RESTRICT&quot;</span>, <span class="hljs-string">&quot;RESTRICT&quot;</span>)
</code></pre> </code></pre>
<h3 id="indexes">Indexes</h3> <h3 id="indexes">Indexes</h3>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Add index</span> <pre><code class="lang-go"><span class="hljs-comment">// Add index</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">AddIndex<span class="token punctuation">(</span></span><span class="token string">&quot;idx_user_name&quot;</span><span class="token operator">,</span> <span class="token string">&quot;name&quot;</span><span class="token operator">)</span> db.Model(&amp;User{}).AddIndex(<span class="hljs-string">&quot;idx_user_name&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>)
<span class="token comment" spellcheck="true">// Multiple column index</span> <span class="hljs-comment">// Multiple column index</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">AddIndex<span class="token punctuation">(</span></span><span class="token string">&quot;idx_user_name_age&quot;</span><span class="token operator">,</span> <span class="token string">&quot;name&quot;</span><span class="token operator">,</span> <span class="token string">&quot;age&quot;</span><span class="token operator">)</span> db.Model(&amp;User{}).AddIndex(<span class="hljs-string">&quot;idx_user_name_age&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;age&quot;</span>)
<span class="token comment" spellcheck="true">// Add unique index</span> <span class="hljs-comment">// Add unique index</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">AddUniqueIndex<span class="token punctuation">(</span></span><span class="token string">&quot;idx_user_name&quot;</span><span class="token operator">,</span> <span class="token string">&quot;name&quot;</span><span class="token operator">)</span> db.Model(&amp;User{}).AddUniqueIndex(<span class="hljs-string">&quot;idx_user_name&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>)
<span class="token comment" spellcheck="true">// Multiple column unique index</span> <span class="hljs-comment">// Multiple column unique index</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">AddUniqueIndex<span class="token punctuation">(</span></span><span class="token string">&quot;idx_user_name_age&quot;</span><span class="token operator">,</span> <span class="token string">&quot;name&quot;</span><span class="token operator">,</span> <span class="token string">&quot;age&quot;</span><span class="token operator">)</span> db.Model(&amp;User{}).AddUniqueIndex(<span class="hljs-string">&quot;idx_user_name_age&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;age&quot;</span>)
<span class="token comment" spellcheck="true">// Remove index</span> <span class="hljs-comment">// Remove index</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span><span class="token operator">.</span><span class="token function">RemoveIndex<span class="token punctuation">(</span></span><span class="token string">&quot;idx_user_name&quot;</span><span class="token operator">)</span> db.Model(&amp;User{}).RemoveIndex(<span class="hljs-string">&quot;idx_user_name&quot;</span>)
</code></pre> </code></pre>
@ -766,7 +766,7 @@ db<span class="token operator">.</span><span class="token function">Model<span c
<script> <script>
require(["gitbook"], function(gitbook) { require(["gitbook"], function(gitbook) {
gitbook.start({"fontsettings":{"theme":"night","family":"sans","size":4},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"prism":{},"anker-enable":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}}); gitbook.start({"fontsettings":{"theme":"night","size":2,"family":"sans"},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"anker-enable":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}});
}); });
</script> </script>

View File

@ -16,7 +16,7 @@
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css"> <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
@ -70,7 +70,7 @@
data-chapter-title="Development" data-chapter-title="Development"
data-filepath="development.md" data-filepath="development.md"
data-basepath="" data-basepath=""
data-revision="Mon Feb 29 2016 21:35:06 GMT+0800 (CST)" data-revision="Mon Feb 29 2016 21:51:55 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -694,7 +694,7 @@ db.Callback().Create().Before(&quot;gorm:create&quot;).After(&quot;gorm:before_c
<script> <script>
require(["gitbook"], function(gitbook) { require(["gitbook"], function(gitbook) {
gitbook.start({"fontsettings":{"theme":"night","family":"sans","size":4},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"prism":{},"anker-enable":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}}); gitbook.start({"fontsettings":{"theme":"night","size":2,"family":"sans"},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"anker-enable":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}});
}); });
</script> </script>

View File

@ -1,13 +1,12 @@
{ {
"title": "GORM Guide", "title": "GORM Guide",
"plugins": [ "plugins": [
"-highlight", "prism", "toc", "github", "edit-link", "anker-enable" "toc", "github", "edit-link", "anker-enable"
], ],
"pluginsConfig": { "pluginsConfig": {
"fontsettings": { "fontsettings": {
"theme": "night", "theme": "night",
"family": "sans", "size": 2
"size": 4
}, },
"toc": { "toc": {
"addClass": true, "addClass": true,

View File

@ -0,0 +1,131 @@
pre,
code {
/* http://jmblog.github.io/color-themes-for-highlightjs */
/* Tomorrow Comment */
/* Tomorrow Red */
/* Tomorrow Orange */
/* Tomorrow Yellow */
/* Tomorrow Green */
/* Tomorrow Aqua */
/* Tomorrow Blue */
/* Tomorrow Purple */
}
pre .hljs-comment,
code .hljs-comment,
pre .hljs-title,
code .hljs-title {
color: #8e908c;
}
pre .hljs-variable,
code .hljs-variable,
pre .hljs-attribute,
code .hljs-attribute,
pre .hljs-tag,
code .hljs-tag,
pre .hljs-regexp,
code .hljs-regexp,
pre .ruby .hljs-constant,
code .ruby .hljs-constant,
pre .xml .hljs-tag .hljs-title,
code .xml .hljs-tag .hljs-title,
pre .xml .hljs-pi,
code .xml .hljs-pi,
pre .xml .hljs-doctype,
code .xml .hljs-doctype,
pre .html .hljs-doctype,
code .html .hljs-doctype,
pre .css .hljs-id,
code .css .hljs-id,
pre .css .hljs-class,
code .css .hljs-class,
pre .css .hljs-pseudo,
code .css .hljs-pseudo {
color: #c82829;
}
pre .hljs-number,
code .hljs-number,
pre .hljs-preprocessor,
code .hljs-preprocessor,
pre .hljs-pragma,
code .hljs-pragma,
pre .hljs-built_in,
code .hljs-built_in,
pre .hljs-literal,
code .hljs-literal,
pre .hljs-params,
code .hljs-params,
pre .hljs-constant,
code .hljs-constant {
color: #f5871f;
}
pre .ruby .hljs-class .hljs-title,
code .ruby .hljs-class .hljs-title,
pre .css .hljs-rules .hljs-attribute,
code .css .hljs-rules .hljs-attribute {
color: #eab700;
}
pre .hljs-string,
code .hljs-string,
pre .hljs-value,
code .hljs-value,
pre .hljs-inheritance,
code .hljs-inheritance,
pre .hljs-header,
code .hljs-header,
pre .ruby .hljs-symbol,
code .ruby .hljs-symbol,
pre .xml .hljs-cdata,
code .xml .hljs-cdata {
color: #718c00;
}
pre .css .hljs-hexcolor,
code .css .hljs-hexcolor {
color: #3e999f;
}
pre .hljs-function,
code .hljs-function,
pre .python .hljs-decorator,
code .python .hljs-decorator,
pre .python .hljs-title,
code .python .hljs-title,
pre .ruby .hljs-function .hljs-title,
code .ruby .hljs-function .hljs-title,
pre .ruby .hljs-title .hljs-keyword,
code .ruby .hljs-title .hljs-keyword,
pre .perl .hljs-sub,
code .perl .hljs-sub,
pre .javascript .hljs-title,
code .javascript .hljs-title,
pre .coffeescript .hljs-title,
code .coffeescript .hljs-title {
color: #4271ae;
}
pre .hljs-keyword,
code .hljs-keyword,
pre .javascript .hljs-function,
code .javascript .hljs-function {
color: #8959a8;
}
pre .hljs,
code .hljs {
display: block;
background: white;
color: #4d4d4c;
padding: 0.5em;
}
pre .coffeescript .javascript,
code .coffeescript .javascript,
pre .javascript .xml,
code .javascript .xml,
pre .tex .hljs-formula,
code .tex .hljs-formula,
pre .xml .javascript,
code .xml .javascript,
pre .xml .vbscript,
code .xml .vbscript,
pre .xml .css,
code .xml .css,
pre .xml .hljs-cdata,
code .xml .hljs-cdata {
opacity: 0.5;
}

View File

@ -0,0 +1,426 @@
.book .book-body .page-wrapper .page-inner section.normal pre,
.book .book-body .page-wrapper .page-inner section.normal code {
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Tomorrow Comment */
/* Tomorrow Red */
/* Tomorrow Orange */
/* Tomorrow Yellow */
/* Tomorrow Green */
/* Tomorrow Aqua */
/* Tomorrow Blue */
/* Tomorrow Purple */
}
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-comment,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-comment,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-title {
color: #8e908c;
}
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-variable,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-variable,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-attribute,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-attribute,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-tag,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-tag,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-regexp,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-regexp,
.book .book-body .page-wrapper .page-inner section.normal pre .ruby .hljs-constant,
.book .book-body .page-wrapper .page-inner section.normal code .ruby .hljs-constant,
.book .book-body .page-wrapper .page-inner section.normal pre .xml .hljs-tag .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal code .xml .hljs-tag .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal pre .xml .hljs-pi,
.book .book-body .page-wrapper .page-inner section.normal code .xml .hljs-pi,
.book .book-body .page-wrapper .page-inner section.normal pre .xml .hljs-doctype,
.book .book-body .page-wrapper .page-inner section.normal code .xml .hljs-doctype,
.book .book-body .page-wrapper .page-inner section.normal pre .html .hljs-doctype,
.book .book-body .page-wrapper .page-inner section.normal code .html .hljs-doctype,
.book .book-body .page-wrapper .page-inner section.normal pre .css .hljs-id,
.book .book-body .page-wrapper .page-inner section.normal code .css .hljs-id,
.book .book-body .page-wrapper .page-inner section.normal pre .css .hljs-class,
.book .book-body .page-wrapper .page-inner section.normal code .css .hljs-class,
.book .book-body .page-wrapper .page-inner section.normal pre .css .hljs-pseudo,
.book .book-body .page-wrapper .page-inner section.normal code .css .hljs-pseudo {
color: #c82829;
}
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-number,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-number,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-preprocessor,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-preprocessor,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-pragma,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-pragma,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-built_in,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-built_in,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-literal,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-literal,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-params,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-params,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-constant,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-constant {
color: #f5871f;
}
.book .book-body .page-wrapper .page-inner section.normal pre .ruby .hljs-class .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal code .ruby .hljs-class .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal pre .css .hljs-rules .hljs-attribute,
.book .book-body .page-wrapper .page-inner section.normal code .css .hljs-rules .hljs-attribute {
color: #eab700;
}
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-string,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-string,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-value,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-value,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-inheritance,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-inheritance,
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-header,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-header,
.book .book-body .page-wrapper .page-inner section.normal pre .ruby .hljs-symbol,
.book .book-body .page-wrapper .page-inner section.normal code .ruby .hljs-symbol,
.book .book-body .page-wrapper .page-inner section.normal pre .xml .hljs-cdata,
.book .book-body .page-wrapper .page-inner section.normal code .xml .hljs-cdata {
color: #718c00;
}
.book .book-body .page-wrapper .page-inner section.normal pre .css .hljs-hexcolor,
.book .book-body .page-wrapper .page-inner section.normal code .css .hljs-hexcolor {
color: #3e999f;
}
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-function,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-function,
.book .book-body .page-wrapper .page-inner section.normal pre .python .hljs-decorator,
.book .book-body .page-wrapper .page-inner section.normal code .python .hljs-decorator,
.book .book-body .page-wrapper .page-inner section.normal pre .python .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal code .python .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal pre .ruby .hljs-function .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal code .ruby .hljs-function .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal pre .ruby .hljs-title .hljs-keyword,
.book .book-body .page-wrapper .page-inner section.normal code .ruby .hljs-title .hljs-keyword,
.book .book-body .page-wrapper .page-inner section.normal pre .perl .hljs-sub,
.book .book-body .page-wrapper .page-inner section.normal code .perl .hljs-sub,
.book .book-body .page-wrapper .page-inner section.normal pre .javascript .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal code .javascript .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal pre .coffeescript .hljs-title,
.book .book-body .page-wrapper .page-inner section.normal code .coffeescript .hljs-title {
color: #4271ae;
}
.book .book-body .page-wrapper .page-inner section.normal pre .hljs-keyword,
.book .book-body .page-wrapper .page-inner section.normal code .hljs-keyword,
.book .book-body .page-wrapper .page-inner section.normal pre .javascript .hljs-function,
.book .book-body .page-wrapper .page-inner section.normal code .javascript .hljs-function {
color: #8959a8;
}
.book .book-body .page-wrapper .page-inner section.normal pre .hljs,
.book .book-body .page-wrapper .page-inner section.normal code .hljs {
display: block;
background: white;
color: #4d4d4c;
padding: 0.5em;
}
.book .book-body .page-wrapper .page-inner section.normal pre .coffeescript .javascript,
.book .book-body .page-wrapper .page-inner section.normal code .coffeescript .javascript,
.book .book-body .page-wrapper .page-inner section.normal pre .javascript .xml,
.book .book-body .page-wrapper .page-inner section.normal code .javascript .xml,
.book .book-body .page-wrapper .page-inner section.normal pre .tex .hljs-formula,
.book .book-body .page-wrapper .page-inner section.normal code .tex .hljs-formula,
.book .book-body .page-wrapper .page-inner section.normal pre .xml .javascript,
.book .book-body .page-wrapper .page-inner section.normal code .xml .javascript,
.book .book-body .page-wrapper .page-inner section.normal pre .xml .vbscript,
.book .book-body .page-wrapper .page-inner section.normal code .xml .vbscript,
.book .book-body .page-wrapper .page-inner section.normal pre .xml .css,
.book .book-body .page-wrapper .page-inner section.normal code .xml .css,
.book .book-body .page-wrapper .page-inner section.normal pre .xml .hljs-cdata,
.book .book-body .page-wrapper .page-inner section.normal code .xml .hljs-cdata {
opacity: 0.5;
}
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code {
/*
Orginal Style from ethanschoonover.com/solarized (c) Jeremy Hull <sourdrums@gmail.com>
*/
/* Solarized Green */
/* Solarized Cyan */
/* Solarized Blue */
/* Solarized Yellow */
/* Solarized Orange */
/* Solarized Red */
/* Solarized Violet */
}
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs {
display: block;
padding: 0.5em;
background: #fdf6e3;
color: #657b83;
}
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-comment,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-comment,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-template_comment,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-template_comment,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .diff .hljs-header,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .diff .hljs-header,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-doctype,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-doctype,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-pi,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-pi,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .lisp .hljs-string,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .lisp .hljs-string,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-javadoc,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-javadoc {
color: #93a1a1;
}
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-keyword,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-keyword,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-winutils,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-winutils,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .method,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .method,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-addition,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-addition,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .css .hljs-tag,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .css .hljs-tag,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-request,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-request,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-status,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-status,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .nginx .hljs-title,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .nginx .hljs-title {
color: #859900;
}
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-number,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-number,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-command,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-command,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-string,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-string,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-tag .hljs-value,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-tag .hljs-value,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-rules .hljs-value,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-rules .hljs-value,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-phpdoc,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-phpdoc,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .tex .hljs-formula,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .tex .hljs-formula,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-regexp,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-regexp,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-hexcolor,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-hexcolor,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-link_url,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-link_url {
color: #2aa198;
}
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-title,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-title,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-localvars,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-localvars,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-chunk,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-chunk,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-decorator,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-decorator,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-built_in,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-built_in,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-identifier,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-identifier,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .vhdl .hljs-literal,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .vhdl .hljs-literal,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-id,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-id,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .css .hljs-function,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .css .hljs-function {
color: #268bd2;
}
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-attribute,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-attribute,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-variable,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-variable,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .lisp .hljs-body,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .lisp .hljs-body,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .smalltalk .hljs-number,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .smalltalk .hljs-number,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-constant,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-constant,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-class .hljs-title,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-class .hljs-title,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-parent,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-parent,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .haskell .hljs-type,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .haskell .hljs-type,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-link_reference,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-link_reference {
color: #b58900;
}
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-preprocessor,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-preprocessor,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-preprocessor .hljs-keyword,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-preprocessor .hljs-keyword,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-pragma,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-pragma,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-shebang,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-shebang,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-symbol,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-symbol,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-symbol .hljs-string,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-symbol .hljs-string,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .diff .hljs-change,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .diff .hljs-change,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-special,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-special,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-attr_selector,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-attr_selector,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-subst,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-subst,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-cdata,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-cdata,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .clojure .hljs-title,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .clojure .hljs-title,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .css .hljs-pseudo,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .css .hljs-pseudo,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-header,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-header {
color: #cb4b16;
}
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-deletion,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-deletion,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-important,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-important {
color: #dc322f;
}
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .hljs-link_label,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .hljs-link_label {
color: #6c71c4;
}
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal pre .tex .hljs-formula,
.book.color-theme-1 .book-body .page-wrapper .page-inner section.normal code .tex .hljs-formula {
background: #eee8d5;
}
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code {
/* Tomorrow Night Bright Theme */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Tomorrow Comment */
/* Tomorrow Red */
/* Tomorrow Orange */
/* Tomorrow Yellow */
/* Tomorrow Green */
/* Tomorrow Aqua */
/* Tomorrow Blue */
/* Tomorrow Purple */
}
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-comment,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-comment,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-title {
color: #969896;
}
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-variable,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-variable,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-attribute,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-attribute,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-tag,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-tag,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-regexp,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-regexp,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .ruby .hljs-constant,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .ruby .hljs-constant,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .xml .hljs-tag .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .xml .hljs-tag .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .xml .hljs-pi,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .xml .hljs-pi,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .xml .hljs-doctype,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .xml .hljs-doctype,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .html .hljs-doctype,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .html .hljs-doctype,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .css .hljs-id,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .css .hljs-id,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .css .hljs-class,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .css .hljs-class,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .css .hljs-pseudo,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .css .hljs-pseudo {
color: #d54e53;
}
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-number,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-number,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-preprocessor,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-preprocessor,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-pragma,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-pragma,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-built_in,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-built_in,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-literal,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-literal,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-params,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-params,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-constant,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-constant {
color: #e78c45;
}
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .ruby .hljs-class .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .ruby .hljs-class .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .css .hljs-rules .hljs-attribute,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .css .hljs-rules .hljs-attribute {
color: #e7c547;
}
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-string,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-string,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-value,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-value,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-inheritance,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-inheritance,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-header,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-header,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .ruby .hljs-symbol,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .ruby .hljs-symbol,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .xml .hljs-cdata,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .xml .hljs-cdata {
color: #b9ca4a;
}
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .css .hljs-hexcolor,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .css .hljs-hexcolor {
color: #70c0b1;
}
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-function,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-function,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .python .hljs-decorator,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .python .hljs-decorator,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .python .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .python .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .ruby .hljs-function .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .ruby .hljs-function .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .ruby .hljs-title .hljs-keyword,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .ruby .hljs-title .hljs-keyword,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .perl .hljs-sub,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .perl .hljs-sub,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .javascript .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .javascript .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .coffeescript .hljs-title,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .coffeescript .hljs-title {
color: #7aa6da;
}
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs-keyword,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs-keyword,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .javascript .hljs-function,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .javascript .hljs-function {
color: #c397d8;
}
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .hljs,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .hljs {
display: block;
background: black;
color: #eaeaea;
padding: 0.5em;
}
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .coffeescript .javascript,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .coffeescript .javascript,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .javascript .xml,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .javascript .xml,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .tex .hljs-formula,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .tex .hljs-formula,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .xml .javascript,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .xml .javascript,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .xml .vbscript,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .xml .vbscript,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .xml .css,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .xml .css,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal pre .xml .hljs-cdata,
.book.color-theme-2 .book-body .page-wrapper .page-inner section.normal code .xml .hljs-cdata {
opacity: 0.5;
}

View File

@ -0,0 +1,11 @@
(function() {
var newEl = document.createElement('script'),
firstScriptTag = document.getElementsByTagName('script')[0];
if (firstScriptTag) {
newEl.async = 1;
newEl.src = '//' + window.location.hostname + ':35729/livereload.js';
firstScriptTag.parentNode.insertBefore(newEl, firstScriptTag);
}
})();

View File

@ -16,7 +16,7 @@
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css"> <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
@ -68,7 +68,7 @@
data-chapter-title="Getting Started with GORM" data-chapter-title="Getting Started with GORM"
data-filepath="README.md" data-filepath="README.md"
data-basepath="" data-basepath=""
data-revision="Mon Feb 29 2016 21:35:06 GMT+0800 (CST)" data-revision="Mon Feb 29 2016 21:51:55 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -602,30 +602,30 @@
<h2 id="install">Install</h2> <h2 id="install">Install</h2>
<pre><code>go get -u github.com/jinzhu/gorm <pre><code>go get -u github.com/jinzhu/gorm
</code></pre><h2 id="basic-usage">Basic Usage</h2> </code></pre><h2 id="basic-usage">Basic Usage</h2>
<pre><code class="lang-go"><span class="token keyword">type</span> Product <span class="token keyword">struct</span> <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">type</span> Product <span class="hljs-keyword">struct</span> {
gorm<span class="token operator">.</span>Model gorm.Model
Code <span class="token builtin">string</span> Code <span class="hljs-typename">string</span>
Price <span class="token builtin">uint</span> Price <span class="hljs-typename">uint</span>
<span class="token operator">}</span> }
<span class="token keyword">var</span> db <span class="token operator">*</span>gorm<span class="token operator">.</span>DB <span class="hljs-keyword">var</span> db *gorm.DB
<span class="token keyword">func</span> <span class="token function">init<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">func</span> init() {
<span class="token keyword">var</span> err <span class="token builtin">error</span> <span class="hljs-keyword">var</span> err error
db<span class="token operator">,</span> err <span class="token operator">=</span> gorm<span class="token operator">.</span><span class="token function">Open<span class="token punctuation">(</span></span><span class="token string">&quot;sqlite&quot;</span><span class="token operator">,</span> <span class="token string">&quot;test.db&quot;</span><span class="token operator">)</span> db, err = gorm.Open(<span class="hljs-string">&quot;sqlite&quot;</span>, <span class="hljs-string">&quot;test.db&quot;</span>)
<span class="token operator">}</span> }
<span class="token keyword">func</span> <span class="token function">main<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token operator">{</span> <span class="hljs-keyword">func</span> main() {
db<span class="token operator">.</span><span class="token function">Create<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>Product<span class="token operator">{</span>Code<span class="token operator">:</span> <span class="token string">&quot;L1212&quot;</span><span class="token operator">,</span> Price<span class="token operator">:</span> <span class="token number">1000</span><span class="token operator">}</span><span class="token operator">)</span> db.Create(&amp;Product{Code: <span class="hljs-string">&quot;L1212&quot;</span>, Price: <span class="hljs-number">1000</span>})
<span class="token keyword">var</span> product Product <span class="hljs-keyword">var</span> product Product
db<span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>product<span class="token operator">,</span> <span class="token number">1</span><span class="token operator">)</span> <span class="token comment" spellcheck="true">// find product with id 1</span> db.First(&amp;product, <span class="hljs-number">1</span>) <span class="hljs-comment">// find product with id 1</span>
db<span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>product<span class="token operator">,</span> <span class="token string">&quot;code = ?&quot;</span><span class="token operator">,</span> <span class="token string">&quot;L1212&quot;</span><span class="token operator">)</span> <span class="token comment" spellcheck="true">// find product with code l1212</span> db.First(&amp;product, <span class="hljs-string">&quot;code = ?&quot;</span>, <span class="hljs-string">&quot;L1212&quot;</span>) <span class="hljs-comment">// find product with code l1212</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>product<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Update<span class="token punctuation">(</span></span><span class="token string">&quot;Price&quot;</span><span class="token operator">,</span> <span class="token number">2000</span><span class="token operator">)</span> <span class="token comment" spellcheck="true">// update product&apos;s price to 2000</span> db.Model(&amp;product).Update(<span class="hljs-string">&quot;Price&quot;</span>, <span class="hljs-number">2000</span>) <span class="hljs-comment">// update product&apos;s price to 2000</span>
db<span class="token operator">.</span><span class="token function">Delete<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>product<span class="token operator">)</span> <span class="token comment" spellcheck="true">// delete product</span> db.Delete(&amp;product) <span class="hljs-comment">// delete product</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h1 id="author">Author</h1> <h1 id="author">Author</h1>
<p><strong>jinzhu</strong></p> <p><strong>jinzhu</strong></p>
@ -691,7 +691,7 @@
<script> <script>
require(["gitbook"], function(gitbook) { require(["gitbook"], function(gitbook) {
gitbook.start({"fontsettings":{"theme":"night","family":"sans","size":4},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"prism":{},"anker-enable":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}}); gitbook.start({"fontsettings":{"theme":"night","size":2,"family":"sans"},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"anker-enable":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}});
}); });
</script> </script>

View File

@ -16,7 +16,7 @@
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css"> <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
@ -70,7 +70,7 @@
data-chapter-title="Models" data-chapter-title="Models"
data-filepath="models.md" data-filepath="models.md"
data-basepath="" data-basepath=""
data-revision="Mon Feb 29 2016 21:35:06 GMT+0800 (CST)" data-revision="Mon Feb 29 2016 21:51:55 GMT+0800 (CST)"
data-innerlanguage=""> data-innerlanguage="">
@ -597,83 +597,83 @@
</ul> </ul>
<!-- toc stop --> <!-- toc stop -->
<h2 id="model-defination">Model Defination</h2> <h2 id="model-defination">Model Defination</h2>
<pre><code class="lang-go"><span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">type</span> User <span class="hljs-keyword">struct</span> {
gorm<span class="token operator">.</span>Model gorm.Model
Birthday time<span class="token operator">.</span>Time Birthday time.Time
Age <span class="token builtin">int</span> Age <span class="hljs-typename">int</span>
Name <span class="token builtin">string</span> <span class="token string">`sql:&quot;size:255&quot;`</span> <span class="token comment" spellcheck="true">// Default size for string is 255, you could reset it with this tag</span> Name <span class="hljs-typename">string</span> <span class="hljs-string">`sql:&quot;size:255&quot;`</span> <span class="hljs-comment">// Default size for string is 255, you could reset it with this tag</span>
Num <span class="token builtin">int</span> <span class="token string">`sql:&quot;AUTO_INCREMENT&quot;`</span> Num <span class="hljs-typename">int</span> <span class="hljs-string">`sql:&quot;AUTO_INCREMENT&quot;`</span>
IgnoreMe <span class="token builtin">int</span> <span class="token string">`sql:&quot;-&quot;`</span> <span class="token comment" spellcheck="true">// Ignore this field</span> IgnoreMe <span class="hljs-typename">int</span> <span class="hljs-string">`sql:&quot;-&quot;`</span> <span class="hljs-comment">// Ignore this field</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h2 id="conventions--overriding-conventions">Conventions &amp; Overriding Conventions</h2> <h2 id="conventions--overriding-conventions">Conventions &amp; Overriding Conventions</h2>
<h3 id="gormmodel-struct"><code>gorm.Model</code> struct</h3> <h3 id="gormmodel-struct"><code>gorm.Model</code> struct</h3>
<p>Gorm has defined struct <code>gorm.Model</code>, which could be embeded in your models, it will add fields <code>ID</code>, <code>CreatedAt</code>, <code>UpdatedAt</code>, <code>DeletedAt</code> to your model</p> <p>Gorm has defined struct <code>gorm.Model</code>, which could be embeded in your models, it will add fields <code>ID</code>, <code>CreatedAt</code>, <code>UpdatedAt</code>, <code>DeletedAt</code> to your model</p>
<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Model&apos;s definition</span> <pre><code class="lang-go"><span class="hljs-comment">// Model&apos;s definition</span>
<span class="token keyword">type</span> Model <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> Model <span class="hljs-keyword">struct</span> {
ID <span class="token builtin">uint</span> <span class="token string">`gorm:&quot;primary_key&quot;`</span> ID <span class="hljs-typename">uint</span> <span class="hljs-string">`gorm:&quot;primary_key&quot;`</span>
CreatedAt time<span class="token operator">.</span>Time CreatedAt time.Time
UpdatedAt time<span class="token operator">.</span>Time UpdatedAt time.Time
DeletedAt <span class="token operator">*</span>time<span class="token operator">.</span>Time DeletedAt *time.Time
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h3 id="table-name-is-the-pluralized-version-of-struct-name">Table name is the pluralized version of struct name</h3> <h3 id="table-name-is-the-pluralized-version-of-struct-name">Table name is the pluralized version of struct name</h3>
<pre><code class="lang-go"><span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span><span class="token operator">}</span> <span class="token comment" spellcheck="true">// default table name is `users`</span> <pre><code class="lang-go"><span class="hljs-keyword">type</span> User <span class="hljs-keyword">struct</span> {} <span class="hljs-comment">// default table name is `users`</span>
<span class="token comment" spellcheck="true">// set User&apos;s table name to be `profiles</span> <span class="hljs-comment">// set User&apos;s table name to be `profiles</span>
<span class="token keyword">type</span> <span class="token operator">(</span>User<span class="token operator">)</span> <span class="token function">TableName<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token builtin">string</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> (User) TableName() <span class="hljs-typename">string</span> {
<span class="token keyword">return</span> <span class="token string">&quot;profiles&quot;</span> <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;profiles&quot;</span>
<span class="token operator">}</span> }
<span class="token keyword">func</span> <span class="token operator">(</span>u User<span class="token operator">)</span> <span class="token function">TableName<span class="token punctuation">(</span></span><span class="token operator">)</span> <span class="token builtin">string</span> <span class="token operator">{</span> <span class="hljs-keyword">func</span> (u User) TableName() <span class="hljs-typename">string</span> {
<span class="token keyword">if</span> u<span class="token operator">.</span>Role <span class="token operator">==</span> <span class="token string">&quot;admin&quot;</span> <span class="token operator">{</span> <span class="hljs-keyword">if</span> u.Role == <span class="hljs-string">&quot;admin&quot;</span> {
<span class="token keyword">return</span> <span class="token string">&quot;admin_users&quot;</span> <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;admin_users&quot;</span>
<span class="token operator">}</span> <span class="token keyword">else</span> <span class="token operator">{</span> } <span class="hljs-keyword">else</span> {
<span class="token keyword">return</span> <span class="token string">&quot;users&quot;</span> <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;users&quot;</span>
<span class="token operator">}</span> }
<span class="token operator">}</span> }
<span class="token comment" spellcheck="true">// Disable table name&apos;s pluralization globally</span> <span class="hljs-comment">// Disable table name&apos;s pluralization globally</span>
db<span class="token operator">.</span><span class="token function">SingularTable<span class="token punctuation">(</span></span><span class="token boolean">true</span><span class="token operator">)</span> <span class="token comment" spellcheck="true">// if set this to true, `User`&apos;s default table name will be `user`, table name setted with `TableName` won&apos;t be affected</span> db.SingularTable(<span class="hljs-constant">true</span>) <span class="hljs-comment">// if set this to true, `User`&apos;s default table name will be `user`, table name setted with `TableName` won&apos;t be affected</span>
</code></pre> </code></pre>
<h3 id="column-name-is-the-snake-case-of-fields-name">Column name is the snake case of field&apos;s name</h3> <h3 id="column-name-is-the-snake-case-of-fields-name">Column name is the snake case of field&apos;s name</h3>
<pre><code class="lang-go"><span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">type</span> User <span class="hljs-keyword">struct</span> {
ID <span class="token builtin">uint</span> <span class="token comment" spellcheck="true">// column name will be `id`</span> ID <span class="hljs-typename">uint</span> <span class="hljs-comment">// column name will be `id`</span>
Name <span class="token builtin">string</span> <span class="token comment" spellcheck="true">// column name will be `name`</span> Name <span class="hljs-typename">string</span> <span class="hljs-comment">// column name will be `name`</span>
Birthday time<span class="token operator">.</span>Time <span class="token comment" spellcheck="true">// column name will be `birthday`</span> Birthday time.Time <span class="hljs-comment">// column name will be `birthday`</span>
CreatedAt time<span class="token operator">.</span>Time <span class="token comment" spellcheck="true">// column name will be `created_at`</span> CreatedAt time.Time <span class="hljs-comment">// column name will be `created_at`</span>
<span class="token operator">}</span> }
<span class="token keyword">type</span> Animal <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> Animal <span class="hljs-keyword">struct</span> {
AnimalId <span class="token builtin">int64</span> <span class="token string">`gorm:&quot;column:beast_id&quot;`</span> <span class="token comment" spellcheck="true">// set column name to `beast_id`</span> AnimalId <span class="hljs-typename">int64</span> <span class="hljs-string">`gorm:&quot;column:beast_id&quot;`</span> <span class="hljs-comment">// set column name to `beast_id`</span>
Birthday time<span class="token operator">.</span>Time <span class="token string">`gorm:&quot;column:day_of_the_beast&quot;`</span> <span class="token comment" spellcheck="true">// set column name to `day_of_the_beast`</span> Birthday time.Time <span class="hljs-string">`gorm:&quot;column:day_of_the_beast&quot;`</span> <span class="hljs-comment">// set column name to `day_of_the_beast`</span>
Age <span class="token builtin">int64</span> <span class="token string">`gorm:&quot;column:age_of_the_beast&quot;`</span> <span class="token comment" spellcheck="true">// set column name to `age_of_the_beast`</span> Age <span class="hljs-typename">int64</span> <span class="hljs-string">`gorm:&quot;column:age_of_the_beast&quot;`</span> <span class="hljs-comment">// set column name to `age_of_the_beast`</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h3 id="field-id-as-primary-key">Field <code>ID</code> as primary key</h3> <h3 id="field-id-as-primary-key">Field <code>ID</code> as primary key</h3>
<pre><code class="lang-go"><span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span> <pre><code class="lang-go"><span class="hljs-keyword">type</span> User <span class="hljs-keyword">struct</span> {
ID <span class="token builtin">uint</span> <span class="token comment" spellcheck="true">// field named `ID` is the default primary key for `User`</span> ID <span class="hljs-typename">uint</span> <span class="hljs-comment">// field named `ID` is the default primary key for `User`</span>
Name <span class="token builtin">string</span> Name <span class="hljs-typename">string</span>
<span class="token operator">}</span> }
<span class="token comment" spellcheck="true">// your could also use tag `primary_key` to set other field as primary key</span> <span class="hljs-comment">// your could also use tag `primary_key` to set other field as primary key</span>
<span class="token keyword">type</span> Animal <span class="token keyword">struct</span> <span class="token operator">{</span> <span class="hljs-keyword">type</span> Animal <span class="hljs-keyword">struct</span> {
AnimalId <span class="token builtin">int64</span> <span class="token string">`gorm:&quot;primary_key&quot;`</span> <span class="token comment" spellcheck="true">// set AnimalId to be primary key</span> AnimalId <span class="hljs-typename">int64</span> <span class="hljs-string">`gorm:&quot;primary_key&quot;`</span> <span class="hljs-comment">// set AnimalId to be primary key</span>
Name <span class="token builtin">string</span> Name <span class="hljs-typename">string</span>
Age <span class="token builtin">int64</span> Age <span class="hljs-typename">int64</span>
<span class="token operator">}</span> }
</code></pre> </code></pre>
<h3 id="field-createdat-used-to-store-records-created-time">Field <code>CreatedAt</code> used to store record&apos;s created time</h3> <h3 id="field-createdat-used-to-store-records-created-time">Field <code>CreatedAt</code> used to store record&apos;s created time</h3>
<p>Create records having <code>CreatedAt</code> field will set it to current time.</p> <p>Create records having <code>CreatedAt</code> field will set it to current time.</p>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Create<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> <span class="token comment" spellcheck="true">// will set `CreatedAt` to current time</span> <pre><code class="lang-go">db.Create(&amp;user) <span class="hljs-comment">// will set `CreatedAt` to current time</span>
<span class="token comment" spellcheck="true">// To change its value, you could use `Update`</span> <span class="hljs-comment">// To change its value, you could use `Update`</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Update<span class="token punctuation">(</span></span><span class="token string">&quot;CreatedAt&quot;</span><span class="token operator">,</span> time<span class="token operator">.</span><span class="token function">Now<span class="token punctuation">(</span></span><span class="token operator">)</span><span class="token operator">)</span> db.Model(&amp;user).Update(<span class="hljs-string">&quot;CreatedAt&quot;</span>, time.Now())
</code></pre> </code></pre>
<h3 id="use-updatedat-used-to-store-records-updated-time">Use <code>UpdatedAt</code> used to store record&apos;s updated time</h3> <h3 id="use-updatedat-used-to-store-records-updated-time">Use <code>UpdatedAt</code> used to store record&apos;s updated time</h3>
<p>Save records having <code>UpdatedAt</code> field will set it to current time.</p> <p>Save records having <code>UpdatedAt</code> field will set it to current time.</p>
<pre><code class="lang-go">db<span class="token operator">.</span><span class="token function">Save<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span> <span class="token comment" spellcheck="true">// will set `UpdatedAt` to current time</span> <pre><code class="lang-go">db.Save(&amp;user) <span class="hljs-comment">// will set `UpdatedAt` to current time</span>
db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&amp;</span>user<span class="token operator">)</span><span class="token operator">.</span><span class="token function">Update<span class="token punctuation">(</span></span><span class="token string">&quot;name&quot;</span><span class="token operator">,</span> <span class="token string">&quot;jinzhu&quot;</span><span class="token operator">)</span> <span class="token comment" spellcheck="true">// will set `UpdatedAt` to current time</span> db.Model(&amp;user).Update(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>) <span class="hljs-comment">// will set `UpdatedAt` to current time</span>
</code></pre> </code></pre>
<h3 id="use-deletedat-to-store-records-deleted-time-if-field-exists">Use <code>DeletedAt</code> to store record&apos;s deleted time if field exists</h3> <h3 id="use-deletedat-to-store-records-deleted-time-if-field-exists">Use <code>DeletedAt</code> to store record&apos;s deleted time if field exists</h3>
<p>Delete records having <code>DeletedAt</code> field, it won&apos;t delete the record from database, but will set field <code>DeletedAt</code>&apos;s value to current time.</p> <p>Delete records having <code>DeletedAt</code> field, it won&apos;t delete the record from database, but will set field <code>DeletedAt</code>&apos;s value to current time.</p>
@ -734,7 +734,7 @@ db<span class="token operator">.</span><span class="token function">Model<span c
<script> <script>
require(["gitbook"], function(gitbook) { require(["gitbook"], function(gitbook) {
gitbook.start({"fontsettings":{"theme":"night","family":"sans","size":4},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"prism":{},"anker-enable":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}}); gitbook.start({"fontsettings":{"theme":"night","size":2,"family":"sans"},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages","label":"Edit This Page"},"anker-enable":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}});
}); });
</script> </script>