705 lines
20 KiB
HTML
705 lines
20 KiB
HTML
<!DOCTYPE HTML>
|
|
<html lang="" >
|
|
<head>
|
|
<title>Getting Started with GORM · GORM Guide</title>
|
|
<meta charset="UTF-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
<meta name="description" content="The fantastic ORM library for Golang, aims to be developer friendly.">
|
|
<meta name="generator" content="GitBook 3.0.0-pre.1">
|
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="gitbook/style.css">
|
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css">
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<meta name="HandheldFriendly" content="true"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
|
|
<meta name="apple-mobile-web-app-capable" content="yes">
|
|
<meta name="apple-mobile-web-app-status-bar-style" content="black">
|
|
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
|
|
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
|
|
|
|
|
|
<link rel="next" href="database.html" />
|
|
|
|
|
|
|
|
</head>
|
|
<body>
|
|
|
|
<div class="book"
|
|
data-level="0"
|
|
data-chapter-title="Getting Started with GORM"
|
|
data-filepath="README.md"
|
|
data-basepath=""
|
|
data-revision="Fri Feb 26 2016 21:50:14 GMT+0800 (CST)"
|
|
data-innerlanguage="">
|
|
|
|
|
|
<div class="book-summary">
|
|
|
|
<nav role="navigation">
|
|
<ul class="summary">
|
|
|
|
|
|
|
|
<li class="chapter active" data-level="0" data-path="./">
|
|
|
|
<a href="./">
|
|
|
|
|
|
Getting Started with GORM
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="1" data-path="database.html">
|
|
|
|
<a href="database.html">
|
|
|
|
|
|
<b>1.</b>
|
|
|
|
Database
|
|
|
|
</a>
|
|
|
|
|
|
|
|
<ul class="articles">
|
|
|
|
|
|
<li class="chapter " data-level="1.1" data-path="database.html">
|
|
|
|
<a href="database.html#connecting-to-a-database">
|
|
|
|
|
|
<b>1.1.</b>
|
|
|
|
Database Connection
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="1.2" data-path="database.html">
|
|
|
|
<a href="database.html#migration">
|
|
|
|
|
|
<b>1.2.</b>
|
|
|
|
Migration
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="2" data-path="models.html">
|
|
|
|
<a href="models.html">
|
|
|
|
|
|
<b>2.</b>
|
|
|
|
Models
|
|
|
|
</a>
|
|
|
|
|
|
|
|
<ul class="articles">
|
|
|
|
|
|
<li class="chapter " data-level="2.1" data-path="models.html">
|
|
|
|
<a href="models.html#model-defination">
|
|
|
|
|
|
<b>2.1.</b>
|
|
|
|
Model Defination
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="2.2" data-path="models.html">
|
|
|
|
<a href="models.html#conventions-overriding-conventions">
|
|
|
|
|
|
<b>2.2.</b>
|
|
|
|
Naming Conventions & Overriding
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="2.3" data-path="associations.html">
|
|
|
|
<a href="associations.html">
|
|
|
|
|
|
<b>2.3.</b>
|
|
|
|
Associations
|
|
|
|
</a>
|
|
|
|
|
|
|
|
<ul class="articles">
|
|
|
|
|
|
<li class="chapter " data-level="2.3.1" data-path="associations.html">
|
|
|
|
<a href="associations.html#belongs-to">
|
|
|
|
|
|
<b>2.3.1.</b>
|
|
|
|
Belongs To
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="2.3.2" data-path="associations.html">
|
|
|
|
<a href="associations.html#has-one">
|
|
|
|
|
|
<b>2.3.2.</b>
|
|
|
|
Has One
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="2.3.3" data-path="associations.html">
|
|
|
|
<a href="associations.html#has-many">
|
|
|
|
|
|
<b>2.3.3.</b>
|
|
|
|
Has Many
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="2.3.4" data-path="associations.html">
|
|
|
|
<a href="associations.html#many-to-many">
|
|
|
|
|
|
<b>2.3.4.</b>
|
|
|
|
Many To Many
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="2.3.5" data-path="associations.html">
|
|
|
|
<a href="associations.html#polymorphism">
|
|
|
|
|
|
<b>2.3.5.</b>
|
|
|
|
Polymorphism
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="2.3.6" data-path="associations.html">
|
|
|
|
<a href="associations.html#association-mode">
|
|
|
|
|
|
<b>2.3.6.</b>
|
|
|
|
Association Mode
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="3" data-path="curd.html">
|
|
|
|
<a href="curd.html">
|
|
|
|
|
|
<b>3.</b>
|
|
|
|
CRUD: Reading and Writing Data
|
|
|
|
</a>
|
|
|
|
|
|
|
|
<ul class="articles">
|
|
|
|
|
|
<li class="chapter " data-level="3.1" data-path="curd.html">
|
|
|
|
<a href="curd.html#create">
|
|
|
|
|
|
<b>3.1.</b>
|
|
|
|
Create
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="3.2" data-path="curd.html">
|
|
|
|
<a href="curd.html#query">
|
|
|
|
|
|
<b>3.2.</b>
|
|
|
|
Query
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="3.3" data-path="curd.html">
|
|
|
|
<a href="curd.html#preloading">
|
|
|
|
|
|
<b>3.3.</b>
|
|
|
|
Preloading (Eager Loading)
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="3.4" data-path="curd.html">
|
|
|
|
<a href="curd.html#update">
|
|
|
|
|
|
<b>3.4.</b>
|
|
|
|
Update
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="3.5" data-path="curd.html">
|
|
|
|
<a href="curd.html#delete">
|
|
|
|
|
|
<b>3.5.</b>
|
|
|
|
Delete / Soft Delete
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="4" data-path="callbacks.html">
|
|
|
|
<a href="callbacks.html">
|
|
|
|
|
|
<b>4.</b>
|
|
|
|
Callbacks
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="5" data-path="advanced.html">
|
|
|
|
<a href="advanced.html">
|
|
|
|
|
|
<b>5.</b>
|
|
|
|
Advanced Usage
|
|
|
|
</a>
|
|
|
|
|
|
|
|
<ul class="articles">
|
|
|
|
|
|
<li class="chapter " data-level="5.1" data-path="advanced.html">
|
|
|
|
<a href="advanced.html#error-handling">
|
|
|
|
|
|
<b>5.1.</b>
|
|
|
|
Error Handling
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="5.2" data-path="advanced.html">
|
|
|
|
<a href="advanced.html#transactions">
|
|
|
|
|
|
<b>5.2.</b>
|
|
|
|
Transactions
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="5.3" data-path="advanced.html">
|
|
|
|
<a href="advanced.html#sql-builder">
|
|
|
|
|
|
<b>5.3.</b>
|
|
|
|
Raw SQL & SQL Builder
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="5.4" data-path="advanced.html">
|
|
|
|
<a href="advanced.html#compose-primary-key">
|
|
|
|
|
|
<b>5.4.</b>
|
|
|
|
Composite Primary Key
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="5.5" data-path="advanced.html">
|
|
|
|
<a href="advanced.html#logger">
|
|
|
|
|
|
<b>5.5.</b>
|
|
|
|
Overriding Logger
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="6" data-path="development.html">
|
|
|
|
<a href="development.html">
|
|
|
|
|
|
<b>6.</b>
|
|
|
|
Development
|
|
|
|
</a>
|
|
|
|
|
|
|
|
<ul class="articles">
|
|
|
|
|
|
<li class="chapter " data-level="6.1" data-path="development.html">
|
|
|
|
<a href="development.html#callbacks">
|
|
|
|
|
|
<b>6.1.</b>
|
|
|
|
Write Plugins
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
|
|
<li class="divider"></li>
|
|
|
|
|
|
<li>
|
|
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
|
|
Published with GitBook
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</nav>
|
|
|
|
</div>
|
|
|
|
<div class="book-body">
|
|
|
|
<div class="body-inner">
|
|
|
|
|
|
<div class="book-header" role="navigation">
|
|
|
|
|
|
<!-- Title -->
|
|
<h1>
|
|
<i class="fa fa-circle-o-notch fa-spin"></i>
|
|
<a href="" >Getting Started with GORM</a>
|
|
</h1>
|
|
</div>
|
|
|
|
|
|
|
|
<div class="page-wrapper" tabindex="-1" role="main">
|
|
<div class="page-inner">
|
|
<section class="normal markdown-section">
|
|
|
|
<h1 id="gorm">GORM</h1>
|
|
<p>The fantastic ORM library for Golang, aims to be developer friendly.</p>
|
|
<p><a href="https://app.wercker.com/project/bykey/0cb7bb1039e21b74f8274941428e0921" target="_blank"><img src="https:/app.wercker.com/status/0cb7bb1039e21b74f8274941428e0921/s/master" alt="wercker status" title="wercker status"></a>
|
|
<a href="https://godoc.org/github.com/jinzhu/gorm" target="_blank"><img src="https:/godoc.org/github.com/jinzhu/gorm?status.svg" alt="GoDoc"></a>
|
|
<a href="https://gitter.im/jinzhu/gorm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge" target="_blank"><img src="https:/badges.gitter.im/Join%20Chat.svg" alt="Join the chat at https://gitter.im/jinzhu/gorm"></a></p>
|
|
<h2 id="overview">Overview</h2>
|
|
<ul>
|
|
<li>Full-Featured ORM (almost)</li>
|
|
<li>Chainable API</li>
|
|
<li>Auto Migrations</li>
|
|
<li>Relations (Has One, Has Many, Belongs To, Many To Many, <a href="#polymorphism">Polymorphism</a>)</li>
|
|
<li>Callbacks (Before/After Create/Save/Update/Delete/Find)</li>
|
|
<li>Preloading (eager loading)</li>
|
|
<li>Transactions</li>
|
|
<li>Embed Anonymous Struct</li>
|
|
<li>Soft Deletes</li>
|
|
<li>Customizable Logger</li>
|
|
<li>Iteration Support via <a href="#row--rows">Rows</a></li>
|
|
<li>Every feature comes with tests</li>
|
|
<li>Developer Friendly</li>
|
|
</ul>
|
|
<h2 id="install">Install</h2>
|
|
<pre><code>go get -u github.com/jinzhu/gorm
|
|
</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>
|
|
gorm<span class="token operator">.</span>Model
|
|
Code <span class="token builtin">string</span>
|
|
Price <span class="token builtin">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="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="token keyword">var</span> err <span class="token builtin">error</span>
|
|
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">"sqlite"</span><span class="token operator">,</span> <span class="token string">"test.db"</span><span class="token operator">)</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>
|
|
db<span class="token operator">.</span><span class="token function">Create<span class="token punctuation">(</span></span><span class="token operator">&</span>Product<span class="token operator">{</span>Code<span class="token operator">:</span> <span class="token string">"L1212"</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>
|
|
|
|
<span class="token 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">&</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<span class="token operator">.</span><span class="token function">First<span class="token punctuation">(</span></span><span class="token operator">&</span>product<span class="token operator">,</span> <span class="token string">"code = ?"</span><span class="token operator">,</span> <span class="token string">"L1212"</span><span class="token operator">)</span> <span class="token comment" spellcheck="true">// 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">&</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">"Price"</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'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">&</span>product<span class="token operator">)</span> <span class="token comment" spellcheck="true">// delete product</span>
|
|
<span class="token operator">}</span>
|
|
</code></pre>
|
|
<h1 id="author">Author</h1>
|
|
<p><strong>jinzhu</strong></p>
|
|
<ul>
|
|
<li><a href="http://github.com/jinzhu" target="_blank">http://github.com/jinzhu</a></li>
|
|
<li><a href="mailto:wosmvp@gmail.com" target="_blank">wosmvp@gmail.com</a></li>
|
|
<li><a href="http://twitter.com/zhangjinzhu" target="_blank">http://twitter.com/zhangjinzhu</a></li>
|
|
</ul>
|
|
<h1 id="contributors">Contributors</h1>
|
|
<p><a href="https://github.com/jinzhu/gorm/graphs/contributors" target="_blank">https://github.com/jinzhu/gorm/graphs/contributors</a></p>
|
|
<h2 id="license">License</h2>
|
|
<p>Released under the <a href="https://github.com/jinzhu/gorm/blob/master/License" target="_blank">MIT License</a>.</p>
|
|
|
|
|
|
</section>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<a href="database.html" class="navigation navigation-next navigation-unique" aria-label="Next page: Database">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<script src="gitbook/app.js"></script>
|
|
|
|
|
|
<script src="gitbook/gitbook-plugin-github/plugin.js"></script>
|
|
|
|
|
|
|
|
<script src="gitbook/gitbook-plugin-edit-link/plugin.js"></script>
|
|
|
|
|
|
|
|
<script src="gitbook/gitbook-plugin-search/lunr.min.js"></script>
|
|
|
|
|
|
|
|
<script src="gitbook/gitbook-plugin-search/search.js"></script>
|
|
|
|
|
|
|
|
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
|
|
|
|
|
|
|
|
<script src="gitbook/gitbook-plugin-fontsettings/buttons.js"></script>
|
|
|
|
|
|
|
|
<script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
|
|
|
|
|
|
<script>
|
|
require(["gitbook"], function(gitbook) {
|
|
gitbook.start({"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/master","label":"Edit This Page"},"prism":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"fontsettings":{"family":"sans","size":2,"theme":"white"},"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"}},"livereload":{}});
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|