Update Pages
This commit is contained in:
parent
af685f64f2
commit
e0cc1afd91
@ -70,7 +70,7 @@
|
|||||||
data-chapter-title="Advanced Usage"
|
data-chapter-title="Advanced Usage"
|
||||||
data-filepath="advanced.md"
|
data-filepath="advanced.md"
|
||||||
data-basepath="."
|
data-basepath="."
|
||||||
data-revision="Wed Mar 09 2016 21:05:14 GMT+0800 (CST)"
|
data-revision="Wed Mar 23 2016 07:30:36 GMT+0800 (CST)"
|
||||||
data-innerlanguage="">
|
data-innerlanguage="">
|
||||||
|
|
||||||
|
|
||||||
|
@ -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="Wed Mar 09 2016 21:05:14 GMT+0800 (CST)"
|
data-revision="Wed Mar 23 2016 07:30:36 GMT+0800 (CST)"
|
||||||
data-innerlanguage="">
|
data-innerlanguage="">
|
||||||
|
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@
|
|||||||
data-chapter-title="Callbacks"
|
data-chapter-title="Callbacks"
|
||||||
data-filepath="callbacks.md"
|
data-filepath="callbacks.md"
|
||||||
data-basepath="."
|
data-basepath="."
|
||||||
data-revision="Wed Mar 09 2016 21:05:14 GMT+0800 (CST)"
|
data-revision="Wed Mar 23 2016 07:30:36 GMT+0800 (CST)"
|
||||||
data-innerlanguage="">
|
data-innerlanguage="">
|
||||||
|
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@
|
|||||||
data-chapter-title="Change Log"
|
data-chapter-title="Change Log"
|
||||||
data-filepath="changelog.md"
|
data-filepath="changelog.md"
|
||||||
data-basepath="."
|
data-basepath="."
|
||||||
data-revision="Wed Mar 09 2016 21:05:14 GMT+0800 (CST)"
|
data-revision="Wed Mar 23 2016 07:30:36 GMT+0800 (CST)"
|
||||||
data-innerlanguage="">
|
data-innerlanguage="">
|
||||||
|
|
||||||
|
|
||||||
|
13
curd.html
13
curd.html
@ -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="Wed Mar 09 2016 21:05:14 GMT+0800 (CST)"
|
data-revision="Wed Mar 23 2016 07:30:36 GMT+0800 (CST)"
|
||||||
data-innerlanguage="">
|
data-innerlanguage="">
|
||||||
|
|
||||||
|
|
||||||
@ -671,9 +671,12 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
<li><a href="#preloading-eager-loading">Preloading (Eager loading)</a><ul>
|
<li><a href="#preloading-eager-loading">Preloading (Eager loading)</a><ul>
|
||||||
|
<li><a href="#custom-preloading-sql">Custom Preloading SQL</a><ul>
|
||||||
<li><a href="#nested-preloading">Nested Preloading</a></li>
|
<li><a href="#nested-preloading">Nested Preloading</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
<li><a href="#update">Update</a><ul>
|
<li><a href="#update">Update</a><ul>
|
||||||
<li><a href="#update-all-fields">Update All Fields</a></li>
|
<li><a href="#update-all-fields">Update All Fields</a></li>
|
||||||
<li><a href="#update-changed-fields">Update Changed Fields</a></li>
|
<li><a href="#update-changed-fields">Update Changed Fields</a></li>
|
||||||
@ -1128,6 +1131,14 @@ db.Preload(<span class="hljs-string">"Orders"</span>).Preload(<span cl
|
|||||||
<span class="hljs-comment">//// SELECT * FROM profiles WHERE user_id IN (1,2,3,4); // has one</span>
|
<span class="hljs-comment">//// SELECT * FROM profiles WHERE user_id IN (1,2,3,4); // has one</span>
|
||||||
<span class="hljs-comment">//// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to</span>
|
<span class="hljs-comment">//// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to</span>
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
<h4 id="custom-preloading-sql">Custom Preloading SQL</h4>
|
||||||
|
<p>You could custom preloading SQL by passing in <code>func(db *gorm.DB) *gorm.DB</code> (same type as the one used for <a href="#scopes">Scopes</a>), for example:</p>
|
||||||
|
<pre><code class="lang-go">db.Preload(<span class="hljs-string">"Orders"</span>, <span class="hljs-keyword">func</span>(db *gorm.DB) *gorm.DB {
|
||||||
|
<span class="hljs-keyword">return</span> db.Order(<span class="hljs-string">"orders.amount DESC"</span>)
|
||||||
|
}).Find(&users)
|
||||||
|
<span class="hljs-comment">//// SELECT * FROM users;</span>
|
||||||
|
<span class="hljs-comment">//// SELECT * FROM orders WHERE user_id IN (1,2,3,4) order by orders.amount DESC;</span>
|
||||||
|
</code></pre>
|
||||||
<h3 id="nested-preloading">Nested Preloading</h3>
|
<h3 id="nested-preloading">Nested Preloading</h3>
|
||||||
<pre><code class="lang-go">db.Preload(<span class="hljs-string">"Orders.OrderItems"</span>).Find(&users)
|
<pre><code class="lang-go">db.Preload(<span class="hljs-string">"Orders.OrderItems"</span>).Find(&users)
|
||||||
db.Preload(<span class="hljs-string">"Orders"</span>, <span class="hljs-string">"state = ?"</span>, <span class="hljs-string">"paid"</span>).Preload(<span class="hljs-string">"Orders.OrderItems"</span>).Find(&users)
|
db.Preload(<span class="hljs-string">"Orders"</span>, <span class="hljs-string">"state = ?"</span>, <span class="hljs-string">"paid"</span>).Preload(<span class="hljs-string">"Orders.OrderItems"</span>).Find(&users)
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||||
<meta name="description" content="When connect to a database, you need to import the database's driver first, for example:">
|
<meta name="description" content="In order to connect to a database, you need to import the database's driver first. For example:">
|
||||||
<meta name="generator" content="GitBook 3.0.0-pre.5">
|
<meta name="generator" content="GitBook 3.0.0-pre.5">
|
||||||
|
|
||||||
|
|
||||||
@ -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="Wed Mar 09 2016 21:05:14 GMT+0800 (CST)"
|
data-revision="Wed Mar 23 2016 07:30:36 GMT+0800 (CST)"
|
||||||
data-innerlanguage="">
|
data-innerlanguage="">
|
||||||
|
|
||||||
|
|
||||||
@ -649,7 +649,7 @@
|
|||||||
</ul>
|
</ul>
|
||||||
<!-- toc stop -->
|
<!-- toc stop -->
|
||||||
<h2 id="connecting-to-a-database">Connecting to a database</h2>
|
<h2 id="connecting-to-a-database">Connecting to a database</h2>
|
||||||
<p>When connect to a database, you need to import the database's driver first, for example:</p>
|
<p>In order to connect to a database, you need to import the database's driver first. For example:</p>
|
||||||
<pre><code class="lang-go"><span class="hljs-keyword">import</span> _ <span class="hljs-string">"github.com/go-sql-driver/mysql"</span>
|
<pre><code class="lang-go"><span class="hljs-keyword">import</span> _ <span class="hljs-string">"github.com/go-sql-driver/mysql"</span>
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>GORM has wrapped some drivers, for easier to remember the import path, so you could import the mysql driver with</p>
|
<p>GORM has wrapped some drivers, for easier to remember the import path, so you could import the mysql driver with</p>
|
||||||
@ -659,7 +659,7 @@
|
|||||||
<span class="hljs-comment">// import _ "github.com/jinzhu/gorm/dialects/mssql"</span>
|
<span class="hljs-comment">// import _ "github.com/jinzhu/gorm/dialects/mssql"</span>
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<h4 id="mysql">MySQL</h4>
|
<h4 id="mysql">MySQL</h4>
|
||||||
<p><strong>NOTE</strong> in order to handle <code>time.Time</code> you need to include <code>parseTime</code> as param <a href="https://github.com/go-sql-driver/mysql#parameters" target="_blank">more supported parameters</a></p>
|
<p><strong>NOTE:</strong> In order to handle <code>time.Time</code>, you need to include <code>parseTime</code> as a parameter. (<a href="https://github.com/go-sql-driver/mysql#parameters" target="_blank">More supported parameters</a>)</p>
|
||||||
<pre><code class="lang-go"><span class="hljs-keyword">import</span> (
|
<pre><code class="lang-go"><span class="hljs-keyword">import</span> (
|
||||||
<span class="hljs-string">"github.com/jinzhu/gorm"</span>
|
<span class="hljs-string">"github.com/jinzhu/gorm"</span>
|
||||||
_ <span class="hljs-string">"github.com/jinzhu/gorm/dialects/mysql"</span>
|
_ <span class="hljs-string">"github.com/jinzhu/gorm/dialects/mysql"</span>
|
||||||
@ -690,12 +690,12 @@
|
|||||||
}
|
}
|
||||||
</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, but you could write a dialect for those unsupported databaes,</p>
|
<p>GORM officially supports the above databases, but you could write a dialect for unsupported databases.</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>To write your own dialect, refer to: <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="migration">Migration</h2>
|
<h2 id="migration">Migration</h2>
|
||||||
<h3 id="auto-migration">Auto Migration</h3>
|
<h3 id="auto-migration">Auto Migration</h3>
|
||||||
<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, missing columns and missing indexes, <strong>WON'T</strong> change existing column's type or delete unused columns to protect your data</p>
|
<p><strong>WARNING:</strong> AutoMigrate will <strong>ONLY</strong> create tables, missing columns and missing indexes, and <strong>WON'T</strong> change existing column's type or delete unused columns to protect your data.</p>
|
||||||
<pre><code class="lang-go">db.AutoMigrate(&User{})
|
<pre><code class="lang-go">db.AutoMigrate(&User{})
|
||||||
|
|
||||||
db.AutoMigrate(&User{}, &Product{}, &Order{})
|
db.AutoMigrate(&User{}, &Product{}, &Order{})
|
||||||
|
@ -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="Wed Mar 09 2016 21:05:14 GMT+0800 (CST)"
|
data-revision="Wed Mar 23 2016 07:30:36 GMT+0800 (CST)"
|
||||||
data-innerlanguage="">
|
data-innerlanguage="">
|
||||||
|
|
||||||
|
|
||||||
|
@ -554,6 +554,18 @@ db.Preload("Orders").Preload("Profile").Preload("Role").Find(&users)
|
|||||||
//// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to
|
//// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Custom Preloading SQL
|
||||||
|
|
||||||
|
You could custom preloading SQL by passing in `func(db *gorm.DB) *gorm.DB` (same type as the one used for [Scopes](#scopes)), for example:
|
||||||
|
|
||||||
|
```go
|
||||||
|
db.Preload("Orders", func(db *gorm.DB) *gorm.DB {
|
||||||
|
return db.Order("orders.amount DESC")
|
||||||
|
}).Find(&users)
|
||||||
|
//// SELECT * FROM users;
|
||||||
|
//// SELECT * FROM orders WHERE user_id IN (1,2,3,4) order by orders.amount DESC;
|
||||||
|
```
|
||||||
|
|
||||||
### Nested Preloading
|
### Nested Preloading
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -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="Wed Mar 09 2016 21:05:14 GMT+0800 (CST)"
|
data-revision="Wed Mar 23 2016 07:30:36 GMT+0800 (CST)"
|
||||||
data-innerlanguage="">
|
data-innerlanguage="">
|
||||||
|
|
||||||
|
|
||||||
|
@ -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="Wed Mar 09 2016 21:05:14 GMT+0800 (CST)"
|
data-revision="Wed Mar 23 2016 07:30:36 GMT+0800 (CST)"
|
||||||
data-innerlanguage="">
|
data-innerlanguage="">
|
||||||
|
|
||||||
|
|
||||||
|
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user