github pages
This commit is contained in:
		
							parent
							
								
									ffe86980cd
								
							
						
					
					
						commit
						bea64d552f
					
				
							
								
								
									
										32
									
								
								SUMMARY.md
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								SUMMARY.md
									
									
									
									
									
								
							@ -1,32 +0,0 @@
 | 
				
			|||||||
# Summary
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
* [Getting Started](getting-started.md)
 | 
					 | 
				
			||||||
* [Database](database.md)
 | 
					 | 
				
			||||||
  * [Connecting to a Database](#connecting-to-a-database)
 | 
					 | 
				
			||||||
  * [Migration]()
 | 
					 | 
				
			||||||
  * [Schema]()
 | 
					 | 
				
			||||||
* [Models]()
 | 
					 | 
				
			||||||
  * [Model Defination]()
 | 
					 | 
				
			||||||
  * [Naming Conventions & Overriding]()
 | 
					 | 
				
			||||||
  * [Associations]()
 | 
					 | 
				
			||||||
    * [Belongs To]()
 | 
					 | 
				
			||||||
    * [Has One]()
 | 
					 | 
				
			||||||
    * [Has Many]()
 | 
					 | 
				
			||||||
    * [Many To Many]()
 | 
					 | 
				
			||||||
    * [Polymorphism]()
 | 
					 | 
				
			||||||
    * [Association Mode]()
 | 
					 | 
				
			||||||
* [CRUD: Reading and Writing Data]()
 | 
					 | 
				
			||||||
  * [Create]()
 | 
					 | 
				
			||||||
  * [Query]()
 | 
					 | 
				
			||||||
    * [Preloading (Eager Loading)]()
 | 
					 | 
				
			||||||
  * [Update]()
 | 
					 | 
				
			||||||
  * [Delete / Soft Delete]()
 | 
					 | 
				
			||||||
  * [Callbacks]()
 | 
					 | 
				
			||||||
* [Advanced Usage]()
 | 
					 | 
				
			||||||
  * [Error Handling]()
 | 
					 | 
				
			||||||
  * [Transactions]()
 | 
					 | 
				
			||||||
  * [Raw SQL & SQL Builder]()
 | 
					 | 
				
			||||||
  * [Composite Primary Key]()
 | 
					 | 
				
			||||||
  * [Overriding Logger]()
 | 
					 | 
				
			||||||
* [Development]()
 | 
					 | 
				
			||||||
  * [Write Plugins]()
 | 
					 | 
				
			||||||
							
								
								
									
										786
									
								
								advanced.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										786
									
								
								advanced.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,786 @@
 | 
				
			|||||||
 | 
					<!DOCTYPE HTML>
 | 
				
			||||||
 | 
					<html lang="" >
 | 
				
			||||||
 | 
					    <head>
 | 
				
			||||||
 | 
					        <title>Advanced Usage · 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="To perform a set of operations within a transaction, the general flow is as below.
 | 
				
			||||||
 | 
					The database handle returned from db.Begin() should be used for all oper">
 | 
				
			||||||
 | 
					        <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="advanced.html" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    <link rel="prev" href="callbacks.html" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    </head>
 | 
				
			||||||
 | 
					    <body>
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					<div class="book"
 | 
				
			||||||
 | 
					    data-level="5"
 | 
				
			||||||
 | 
					    data-chapter-title="Advanced Usage"
 | 
				
			||||||
 | 
					    data-filepath="advanced.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 " 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 active" 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="" >Advanced Usage</a>
 | 
				
			||||||
 | 
					    </h1>
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <div class="page-wrapper" tabindex="-1" role="main">
 | 
				
			||||||
 | 
					                        <div class="page-inner">
 | 
				
			||||||
 | 
					                            <section class="normal markdown-section">
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            <h1 id="advanced-usage">Advanced Usage</h1>
 | 
				
			||||||
 | 
					<!-- toc -->
 | 
				
			||||||
 | 
					<ul>
 | 
				
			||||||
 | 
					<li><a href="#error-handling">Error Handling</a></li>
 | 
				
			||||||
 | 
					<li><a href="#transactions">Transactions</a><ul>
 | 
				
			||||||
 | 
					<li><a href="#a-specific-example">A Specific Example</a></li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					</li>
 | 
				
			||||||
 | 
					<li><a href="#raw-sql">Raw SQL</a></li>
 | 
				
			||||||
 | 
					<li><a href="#row-rows">Row & Rows</a><ul>
 | 
				
			||||||
 | 
					<li><a href="#scan-rows">Scan Rows</a></li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					</li>
 | 
				
			||||||
 | 
					<li><a href="#composite-primary-key">Composite Primary Key</a></li>
 | 
				
			||||||
 | 
					<li><a href="#logger">Logger</a><ul>
 | 
				
			||||||
 | 
					<li><a href="#customize-logger">Customize Logger</a></li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					</li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					<!-- toc stop -->
 | 
				
			||||||
 | 
					<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">"name = ?"</span><span class="token operator">,</span> <span class="token string">"jinzhu"</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">&</span>user<span class="token operator">)</span>
 | 
				
			||||||
 | 
					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">&</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">&</span>users<span class="token operator">)</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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="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">"name = ?"</span><span class="token operator">,</span> <span class="token string">"jinzhu"</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">&</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="token comment" spellcheck="true">// error handling...</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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>
 | 
				
			||||||
 | 
					db<span class="token operator">.</span><span class="token function">Where<span class="token punctuation">(</span></span><span class="token string">"name = ?"</span><span class="token operator">,</span> <span class="token string">"hello world"</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">&</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
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">"name = ?"</span><span class="token operator">,</span> <span class="token string">"hello world"</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">&</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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<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">&</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">&</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="token comment" spellcheck="true">// no credit card found error handling</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h2 id="transactions">Transactions</h2>
 | 
				
			||||||
 | 
					<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.
 | 
				
			||||||
 | 
					(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>
 | 
				
			||||||
 | 
					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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// do some database operations (use 'tx' from this point, not 'db')</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>
 | 
				
			||||||
 | 
					<span class="token operator">...</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					  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>
 | 
				
			||||||
 | 
					  <span class="token comment" spellcheck="true">// 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">&</span>Animal<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">"Giraffe"</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>
 | 
				
			||||||
 | 
					     tx<span class="token operator">.</span><span class="token function">Rollback<span class="token punctuation">(</span></span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					     <span class="token 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">&</span>Animal<span class="token operator">{</span>Name<span class="token operator">:</span> <span class="token string">"Lion"</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>
 | 
				
			||||||
 | 
					     tx<span class="token operator">.</span><span class="token function">Rollback<span class="token punctuation">(</span></span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					     <span class="token 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>
 | 
				
			||||||
 | 
					  <span class="token keyword">return</span> <span class="token boolean">nil</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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">"DROP TABLE users;"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					db<span class="token operator">.</span><span class="token function">Exec<span class="token punctuation">(</span></span><span class="token string">"UPDATE orders SET shipped_at=? WHERE id IN (?)"</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>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h2 id="row--rows">Row & Rows</h2>
 | 
				
			||||||
 | 
					<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">"users"</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">"name = ?"</span><span class="token operator">,</span> <span class="token string">"jinzhu"</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">"name, age"</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>
 | 
				
			||||||
 | 
					row<span class="token operator">.</span><span class="token function">Scan<span class="token punctuation">(</span></span><span class="token operator">&</span>name<span class="token operator">,</span> <span class="token operator">&</span>age<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">Model<span class="token punctuation">(</span></span><span class="token operator">&</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">"name = ?"</span><span class="token operator">,</span> <span class="token string">"jinzhu"</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">"name, age, email"</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>
 | 
				
			||||||
 | 
					<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="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="token operator">...</span>
 | 
				
			||||||
 | 
					    rows<span class="token operator">.</span><span class="token function">Scan<span class="token punctuation">(</span></span><span class="token operator">&</span>name<span class="token operator">,</span> <span class="token operator">&</span>age<span class="token operator">,</span> <span class="token operator">&</span>email<span class="token operator">)</span>
 | 
				
			||||||
 | 
					    <span class="token operator">...</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">"select name, age, email from users where name = ?"</span><span class="token operator">,</span> <span class="token string">"jinzhu"</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>
 | 
				
			||||||
 | 
					<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="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="token operator">...</span>
 | 
				
			||||||
 | 
					    rows<span class="token operator">.</span><span class="token function">Scan<span class="token punctuation">(</span></span><span class="token operator">&</span>name<span class="token operator">,</span> <span class="token operator">&</span>age<span class="token operator">,</span> <span class="token operator">&</span>email<span class="token operator">)</span>
 | 
				
			||||||
 | 
					    <span class="token operator">...</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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">&</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">"name = ?"</span><span class="token operator">,</span> <span class="token string">"jinzhu"</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">"name, age, email"</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>
 | 
				
			||||||
 | 
					<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="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="token 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">&</span>user<span class="token operator">)</span>
 | 
				
			||||||
 | 
					  <span class="token comment" spellcheck="true">// do something</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					    ID           <span class="token builtin">string</span> <span class="token string">`gorm:"primary_key"`</span>
 | 
				
			||||||
 | 
					    LanguageCode <span class="token builtin">string</span> <span class="token string">`gorm:"primary_key"`</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h2 id="logger">Logger</h2>
 | 
				
			||||||
 | 
					<p>Gorm has built-in logger support</p>
 | 
				
			||||||
 | 
					<pre><code class="lang-go"><span class="token comment" spellcheck="true">// 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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">"name = ?"</span><span class="token operator">,</span> <span class="token string">"jinzhu"</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">&</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<p><img src="https:/raw.github.com/jinzhu/gorm/master/doc/logger.png" alt="logger"></p>
 | 
				
			||||||
 | 
					<h3 id="customize-logger">Customize Logger</h3>
 | 
				
			||||||
 | 
					<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Refer gorm'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<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">"\r\n"</span><span class="token operator">,</span> <span class="token number">0</span><span class="token operator">)</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            </section>
 | 
				
			||||||
 | 
					                        </div>
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="callbacks.html" class="navigation navigation-prev " aria-label="Previous page: Callbacks">
 | 
				
			||||||
 | 
					                    <i class="fa fa-angle-left"></i>
 | 
				
			||||||
 | 
					                </a>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="advanced.html" class="navigation navigation-next " aria-label="Next page: Error Handling">
 | 
				
			||||||
 | 
					                    <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>
 | 
				
			||||||
							
								
								
									
										787
									
								
								associations.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										787
									
								
								associations.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,787 @@
 | 
				
			|||||||
 | 
					<!DOCTYPE HTML>
 | 
				
			||||||
 | 
					<html lang="" >
 | 
				
			||||||
 | 
					    <head>
 | 
				
			||||||
 | 
					        <title>Associations · 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="Supports polymorphic has-many and has-one associations.">
 | 
				
			||||||
 | 
					        <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="associations.html" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    <link rel="prev" href="models.html" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    </head>
 | 
				
			||||||
 | 
					    <body>
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					<div class="book"
 | 
				
			||||||
 | 
					    data-level="2.3"
 | 
				
			||||||
 | 
					    data-chapter-title="Associations"
 | 
				
			||||||
 | 
					    data-filepath="associations.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 " 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 active" 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="" >Associations</a>
 | 
				
			||||||
 | 
					    </h1>
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <div class="page-wrapper" tabindex="-1" role="main">
 | 
				
			||||||
 | 
					                        <div class="page-inner">
 | 
				
			||||||
 | 
					                            <section class="normal markdown-section">
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            <h1 id="associations">Associations</h1>
 | 
				
			||||||
 | 
					<!-- toc -->
 | 
				
			||||||
 | 
					<ul>
 | 
				
			||||||
 | 
					<li><a href="#belongs-to">Belongs To</a></li>
 | 
				
			||||||
 | 
					<li><a href="#has-one">Has One</a></li>
 | 
				
			||||||
 | 
					<li><a href="#has-many">Has Many</a></li>
 | 
				
			||||||
 | 
					<li><a href="#many-to-many">Many To Many</a></li>
 | 
				
			||||||
 | 
					<li><a href="#polymorphism">Polymorphism</a></li>
 | 
				
			||||||
 | 
					<li><a href="#association-mode">Association Mode</a></li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					<!-- toc stop -->
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					  gorm<span class="token operator">.</span>Model
 | 
				
			||||||
 | 
					  Profile   Profile
 | 
				
			||||||
 | 
					  ProfileID <span class="token builtin">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>
 | 
				
			||||||
 | 
					  gorm<span class="token operator">.</span>Model
 | 
				
			||||||
 | 
					  Name   <span class="token builtin">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">&</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">&</span>profile<span class="token operator">)</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">//// SELECT * FROM profiles WHERE id = 111; // 111 is user's foreign key ProfileID</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					    gorm<span class="token operator">.</span>Model
 | 
				
			||||||
 | 
					    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>
 | 
				
			||||||
 | 
					    gorm<span class="token operator">.</span>Model
 | 
				
			||||||
 | 
					    UserID   <span class="token builtin">uint</span>
 | 
				
			||||||
 | 
					    Number   <span class="token builtin">string</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token 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">&</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">&</span>card<span class="token operator">,</span> <span class="token string">"CreditCard"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">//// SELECT * FROM credit_cards WHERE user_id = 123; // 123 is user's primary key</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// CreditCard is user's field name, it means get user'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'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">&</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">&</span>card<span class="token operator">)</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					    gorm<span class="token operator">.</span>Model
 | 
				
			||||||
 | 
					    Emails   <span class="token operator">[</span><span class="token operator">]</span>Email
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token keyword">type</span> Email <span class="token keyword">struct</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					    gorm<span class="token operator">.</span>Model
 | 
				
			||||||
 | 
					    Email   <span class="token builtin">string</span>
 | 
				
			||||||
 | 
					    UserID  <span class="token builtin">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">&</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">&</span>emails<span class="token operator">)</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">//// SELECT * FROM emails WHERE user_id = 111; // 111 is user's primary key</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					    gorm<span class="token operator">.</span>Model
 | 
				
			||||||
 | 
					    Languages         <span class="token operator">[</span><span class="token operator">]</span>Language <span class="token string">`gorm:"many2many:user_languages;"`</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token keyword">type</span> Language <span class="token keyword">struct</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					    gorm<span class="token operator">.</span>Model
 | 
				
			||||||
 | 
					    Name <span class="token builtin">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">&</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">&</span>languages<span class="token operator">)</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">//// SELECT * FROM "languages" INNER JOIN "user_languages" ON "user_languages"."language_id" = "languages"."id" WHERE "user_languages"."user_id" = 111</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h2 id="polymorphism">Polymorphism</h2>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					    Id    <span class="token builtin">int</span>
 | 
				
			||||||
 | 
					    Name  <span class="token builtin">string</span>
 | 
				
			||||||
 | 
					    Toy   Toy <span class="token string">`gorm:"polymorphic:Owner;"`</span>
 | 
				
			||||||
 | 
					  <span class="token operator">}</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  <span class="token keyword">type</span> Dog <span class="token keyword">struct</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					    Id   <span class="token builtin">int</span>
 | 
				
			||||||
 | 
					    Name <span class="token builtin">string</span>
 | 
				
			||||||
 | 
					    Toy  Toy <span class="token string">`gorm:"polymorphic:Owner;"`</span>
 | 
				
			||||||
 | 
					  <span class="token operator">}</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  <span class="token keyword">type</span> Toy <span class="token keyword">struct</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					    Id        <span class="token builtin">int</span>
 | 
				
			||||||
 | 
					    Name      <span class="token builtin">string</span>
 | 
				
			||||||
 | 
					    OwnerId   <span class="token builtin">int</span>
 | 
				
			||||||
 | 
					    OwnerType <span class="token builtin">string</span>
 | 
				
			||||||
 | 
					  <span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					<span class="token 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">&</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">"Languages"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// `user` is the source, it need to be a valid record (contains primary key)</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// `Languages` is source'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="token comment" spellcheck="true">// db.Model(&user).Association("Languages").Error</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">&</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">"Languages"</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">&</span>languages<span class="token operator">)</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">&</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">"Languages"</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<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&</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">"Languages"</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">"DE"</span><span class="token operator">}</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// Delete - Remove relationship between source & passed arguments, won'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">&</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">"Languages"</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<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&</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">"Languages"</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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">&</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">"Languages"</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<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&</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">"Languages"</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">"DE"</span><span class="token operator">}</span><span class="token operator">,</span> languageEN<span class="token operator">)</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">&</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">"Languages"</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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// Clear - Remove relationship between source & current associations, won'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">&</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">"Languages"</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>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            </section>
 | 
				
			||||||
 | 
					                        </div>
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="models.html" class="navigation navigation-prev " aria-label="Previous page: Naming Conventions & Overriding">
 | 
				
			||||||
 | 
					                    <i class="fa fa-angle-left"></i>
 | 
				
			||||||
 | 
					                </a>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="associations.html" class="navigation navigation-next " aria-label="Next page: Belongs To">
 | 
				
			||||||
 | 
					                    <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>
 | 
				
			||||||
							
								
								
									
										717
									
								
								callbacks.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										717
									
								
								callbacks.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,717 @@
 | 
				
			|||||||
 | 
					<!DOCTYPE HTML>
 | 
				
			||||||
 | 
					<html lang="" >
 | 
				
			||||||
 | 
					    <head>
 | 
				
			||||||
 | 
					        <title>Callbacks · 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="Callbacks are methods defined on the pointer of struct.
 | 
				
			||||||
 | 
					If any callback returns an error, gorm will stop future operations and rollback all changes.">
 | 
				
			||||||
 | 
					        <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="advanced.html" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    <link rel="prev" href="curd.html" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    </head>
 | 
				
			||||||
 | 
					    <body>
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					<div class="book"
 | 
				
			||||||
 | 
					    data-level="4"
 | 
				
			||||||
 | 
					    data-chapter-title="Callbacks"
 | 
				
			||||||
 | 
					    data-filepath="callbacks.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 " 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 active" 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="" >Callbacks</a>
 | 
				
			||||||
 | 
					    </h1>
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <div class="page-wrapper" tabindex="-1" role="main">
 | 
				
			||||||
 | 
					                        <div class="page-inner">
 | 
				
			||||||
 | 
					                            <section class="normal markdown-section">
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            <h1 id="callbacks">Callbacks</h1>
 | 
				
			||||||
 | 
					<!-- toc -->
 | 
				
			||||||
 | 
					<ul>
 | 
				
			||||||
 | 
					<li><a href="#creating-an-object">Creating An Object</a></li>
 | 
				
			||||||
 | 
					<li><a href="#updating-an-object">Updating An Object</a></li>
 | 
				
			||||||
 | 
					<li><a href="#destroying-an-object">Destroying An Object</a></li>
 | 
				
			||||||
 | 
					<li><a href="#after-find">After Find</a></li>
 | 
				
			||||||
 | 
					<li><a href="#example">Example</a></li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					<!-- toc stop -->
 | 
				
			||||||
 | 
					<p>Callbacks are methods defined on the pointer of struct.
 | 
				
			||||||
 | 
					If any callback returns an error, gorm will stop future operations and rollback all changes.</p>
 | 
				
			||||||
 | 
					<p>Here is the list of all available callbacks:
 | 
				
			||||||
 | 
					(listed in the same order in which they will get called during the respective operations)</p>
 | 
				
			||||||
 | 
					<h3 id="creating-an-object">Creating An Object</h3>
 | 
				
			||||||
 | 
					<pre><code class="lang-go">BeforeSave
 | 
				
			||||||
 | 
					BeforeCreate
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// save before associations</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// save self</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// save after associations</span>
 | 
				
			||||||
 | 
					AfterCreate
 | 
				
			||||||
 | 
					AfterSave
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h3 id="updating-an-object">Updating An Object</h3>
 | 
				
			||||||
 | 
					<pre><code class="lang-go">BeforeSave
 | 
				
			||||||
 | 
					BeforeUpdate
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// save before associations</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// save self</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// save after associations</span>
 | 
				
			||||||
 | 
					AfterUpdate
 | 
				
			||||||
 | 
					AfterSave
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h3 id="destroying-an-object">Destroying An Object</h3>
 | 
				
			||||||
 | 
					<pre><code class="lang-go">BeforeDelete
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// delete self</span>
 | 
				
			||||||
 | 
					AfterDelete
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h3 id="after-find">After Find</h3>
 | 
				
			||||||
 | 
					<pre><code class="lang-go"><span class="token comment" spellcheck="true">// load data from database</span>
 | 
				
			||||||
 | 
					AfterFind
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					    <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>
 | 
				
			||||||
 | 
					        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">"read only user"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					    <span class="token operator">}</span>
 | 
				
			||||||
 | 
					    <span class="token keyword">return</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// Rollback the insertion if user'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="token keyword">if</span> <span class="token operator">(</span>u<span class="token operator">.</span>Id <span class="token operator">></span> <span class="token number">1000</span><span class="token operator">)</span> <span class="token operator">{</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">"user id is already greater than 1000"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					    <span class="token operator">}</span>
 | 
				
			||||||
 | 
					    <span class="token keyword">return</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<p>Save/delete operations in gorm are running in a transaction.
 | 
				
			||||||
 | 
					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.
 | 
				
			||||||
 | 
					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>
 | 
				
			||||||
 | 
					    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">"role"</span><span class="token operator">,</span> <span class="token string">"admin"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					    <span class="token keyword">return</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            </section>
 | 
				
			||||||
 | 
					                        </div>
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="curd.html" class="navigation navigation-prev " aria-label="Previous page: Delete / Soft Delete">
 | 
				
			||||||
 | 
					                    <i class="fa fa-angle-left"></i>
 | 
				
			||||||
 | 
					                </a>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="advanced.html" class="navigation navigation-next " aria-label="Next page: Advanced Usage">
 | 
				
			||||||
 | 
					                    <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>
 | 
				
			||||||
							
								
								
									
										779
									
								
								database.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										779
									
								
								database.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,779 @@
 | 
				
			|||||||
 | 
					<!DOCTYPE HTML>
 | 
				
			||||||
 | 
					<html lang="" >
 | 
				
			||||||
 | 
					    <head>
 | 
				
			||||||
 | 
					        <title>Database · 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="NOTE don't forgot params parseTime to handle data type time.Time, more support parameters">
 | 
				
			||||||
 | 
					        <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" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    <link rel="prev" href="./" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    </head>
 | 
				
			||||||
 | 
					    <body>
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					<div class="book"
 | 
				
			||||||
 | 
					    data-level="1"
 | 
				
			||||||
 | 
					    data-chapter-title="Database"
 | 
				
			||||||
 | 
					    data-filepath="database.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 " data-level="0" data-path="./">
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					                <a href="./">
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					                    
 | 
				
			||||||
 | 
					                    Getting Started with GORM
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					                </a>
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					        </li>
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					        <li class="chapter active" 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="" >Database</a>
 | 
				
			||||||
 | 
					    </h1>
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <div class="page-wrapper" tabindex="-1" role="main">
 | 
				
			||||||
 | 
					                        <div class="page-inner">
 | 
				
			||||||
 | 
					                            <section class="normal markdown-section">
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            <h1 id="database">Database</h1>
 | 
				
			||||||
 | 
					<!-- toc -->
 | 
				
			||||||
 | 
					<ul>
 | 
				
			||||||
 | 
					<li><a href="#connecting-to-a-database">Connecting to a database</a><ul>
 | 
				
			||||||
 | 
					<li><a href="#mysql">MySQL</a></li>
 | 
				
			||||||
 | 
					<li><a href="#postgresql">PostgreSQL</a></li>
 | 
				
			||||||
 | 
					<li><a href="#sqlite3">Sqlite3</a></li>
 | 
				
			||||||
 | 
					<li><a href="#write-dialect-for-unsupported-databases">Write Dialect for unsupported databases</a></li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					</li>
 | 
				
			||||||
 | 
					<li><a href="#generic-database-object-sqldb">Generic database object *sql.DB</a></li>
 | 
				
			||||||
 | 
					<li><a href="#migration">Migration</a><ul>
 | 
				
			||||||
 | 
					<li><a href="#auto-migration">Auto Migration</a></li>
 | 
				
			||||||
 | 
					<li><a href="#has-table">Has Table</a></li>
 | 
				
			||||||
 | 
					<li><a href="#create-table">Create Table</a></li>
 | 
				
			||||||
 | 
					<li><a href="#drop-table">Drop table</a></li>
 | 
				
			||||||
 | 
					<li><a href="#modifycolumn">ModifyColumn</a></li>
 | 
				
			||||||
 | 
					<li><a href="#dropcolumn">DropColumn</a></li>
 | 
				
			||||||
 | 
					<li><a href="#add-foreign-key">Add Foreign Key</a></li>
 | 
				
			||||||
 | 
					<li><a href="#indexes">Indexes</a></li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					</li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					<!-- toc stop -->
 | 
				
			||||||
 | 
					<h2 id="connecting-to-a-database">Connecting to a database</h2>
 | 
				
			||||||
 | 
					<h4 id="mysql">MySQL</h4>
 | 
				
			||||||
 | 
					<p><strong>NOTE</strong> don'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>
 | 
				
			||||||
 | 
					    <span class="token string">"github.com/jinzhu/gorm"</span>
 | 
				
			||||||
 | 
					    <span class="token boolean">_</span> <span class="token string">"github.com/go-sql-driver/mysql"</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> 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">"mysql"</span><span class="token operator">,</span> <span class="token string">"user:password@/dbname?charset=utf8&parseTime=True&loc=Local"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h4 id="postgresql">PostgreSQL</h4>
 | 
				
			||||||
 | 
					<pre><code class="lang-go"><span class="token keyword">import</span> <span class="token operator">(</span>
 | 
				
			||||||
 | 
					    <span class="token string">"github.com/jinzhu/gorm"</span>
 | 
				
			||||||
 | 
					    <span class="token boolean">_</span> <span class="token string">"github.com/lib/pq"</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> 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">"postgres"</span><span class="token operator">,</span> <span class="token string">"user=gorm dbname=gorm sslmode=disable"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h4 id="sqlite3">Sqlite3</h4>
 | 
				
			||||||
 | 
					<pre><code class="lang-go"><span class="token keyword">import</span> <span class="token operator">(</span>
 | 
				
			||||||
 | 
					    <span class="token string">"github.com/jinzhu/gorm"</span>
 | 
				
			||||||
 | 
					    <span class="token boolean">_</span> <span class="token string">"github.com/mattn/go-sqlite3"</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> 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">"sqlite3"</span><span class="token operator">,</span> <span class="token string">"/tmp/gorm.db"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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>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>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					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 comment" spellcheck="true">// 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<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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  <span class="token comment" spellcheck="true">// 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>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h2 id="migration">Migration</h2>
 | 
				
			||||||
 | 
					<!-- toc -->
 | 
				
			||||||
 | 
					<h3 id="auto-migration">Auto Migration</h3>
 | 
				
			||||||
 | 
					<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't exist,
 | 
				
			||||||
 | 
					WON'T change existing column'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">&</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					db<span class="token operator">.</span><span class="token function">AutoMigrate<span class="token punctuation">(</span></span><span class="token operator">&</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">,</span> <span class="token operator">&</span>Product<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">,</span> <span class="token operator">&</span>Order<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">"gorm:table_options"</span><span class="token operator">,</span> <span class="token string">"ENGINE=InnoDB"</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">&</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h3 id="has-table">Has Table</h3>
 | 
				
			||||||
 | 
					<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Check if model `User`'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">&</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">"users"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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">&</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					db<span class="token operator">.</span><span class="token function">Set<span class="token punctuation">(</span></span><span class="token string">"gorm:table_options"</span><span class="token operator">,</span> <span class="token string">"ENGINE=InnoDB"</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">&</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// will append "ENGINE=InnoDB" to the SQL statement when creating table `users`</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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">&</span>User<span class="token operator">{</span><span class="token operator">}</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h3 id="modifycolumn">ModifyColumn</h3>
 | 
				
			||||||
 | 
					<p>Change column's type</p>
 | 
				
			||||||
 | 
					<pre><code class="lang-go"><span class="token comment" spellcheck="true">// change column description's data type to `text` for model `User`'s table</span>
 | 
				
			||||||
 | 
					db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&</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">"description"</span><span class="token operator">,</span> <span class="token string">"text"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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">&</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">"description"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h3 id="add-foreign-key">Add Foreign Key</h3>
 | 
				
			||||||
 | 
					<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Add foreign key</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 1st param : foreignkey field</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 2nd param : destination table(id)</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 3rd param : ONDELETE</span>
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">&</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">"city_id"</span><span class="token operator">,</span> <span class="token string">"cities(id)"</span><span class="token operator">,</span> <span class="token string">"RESTRICT"</span><span class="token operator">,</span> <span class="token string">"RESTRICT"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h3 id="indexes">Indexes</h3>
 | 
				
			||||||
 | 
					<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Add index</span>
 | 
				
			||||||
 | 
					db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&</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">"idx_user_name"</span><span class="token operator">,</span> <span class="token string">"name"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">&</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">"idx_user_name_age"</span><span class="token operator">,</span> <span class="token string">"name"</span><span class="token operator">,</span> <span class="token string">"age"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">&</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">"idx_user_name"</span><span class="token operator">,</span> <span class="token string">"name"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">&</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">"idx_user_name_age"</span><span class="token operator">,</span> <span class="token string">"name"</span><span class="token operator">,</span> <span class="token string">"age"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// Remove index</span>
 | 
				
			||||||
 | 
					db<span class="token operator">.</span><span class="token function">Model<span class="token punctuation">(</span></span><span class="token operator">&</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">"idx_user_name"</span><span class="token operator">)</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            </section>
 | 
				
			||||||
 | 
					                        </div>
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="./" class="navigation navigation-prev " aria-label="Previous page: Getting Started with GORM">
 | 
				
			||||||
 | 
					                    <i class="fa fa-angle-left"></i>
 | 
				
			||||||
 | 
					                </a>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="database.html" class="navigation navigation-next " aria-label="Next page: Database Connection">
 | 
				
			||||||
 | 
					                    <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>
 | 
				
			||||||
							
								
								
									
										31
									
								
								database.md
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								database.md
									
									
									
									
									
								
							@ -1,31 +0,0 @@
 | 
				
			|||||||
## Connecting To A Database
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
```go
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
    "github.com/jinzhu/gorm"
 | 
					 | 
				
			||||||
    _ "github.com/lib/pq"
 | 
					 | 
				
			||||||
    _ "github.com/go-sql-driver/mysql"
 | 
					 | 
				
			||||||
    _ "github.com/mattn/go-sqlite3"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func init() {
 | 
					 | 
				
			||||||
  db, err := gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
 | 
					 | 
				
			||||||
  // db, err := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
 | 
					 | 
				
			||||||
  // db, err := gorm.Open("sqlite3", "/tmp/gorm.db")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // You can also use an existing database connection handle
 | 
					 | 
				
			||||||
  // dbSql, _ := sql.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
 | 
					 | 
				
			||||||
  // db, _ := gorm.Open("postgres", dbSql)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // Get database connection handle [*sql.DB](http://golang.org/pkg/database/sql/#DB)
 | 
					 | 
				
			||||||
  db.DB()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // Then you could invoke `*sql.DB`'s functions with it
 | 
					 | 
				
			||||||
  db.DB().Ping()
 | 
					 | 
				
			||||||
  db.DB().SetMaxIdleConns(10)
 | 
					 | 
				
			||||||
  db.DB().SetMaxOpenConns(100)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // Disable table name's pluralization
 | 
					 | 
				
			||||||
  db.SingularTable(true)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
							
								
								
									
										707
									
								
								development.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										707
									
								
								development.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,707 @@
 | 
				
			|||||||
 | 
					<!DOCTYPE HTML>
 | 
				
			||||||
 | 
					<html lang="" >
 | 
				
			||||||
 | 
					    <head>
 | 
				
			||||||
 | 
					        <title>Development · 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 most notable component of Gorm isgorm.DB, which hold database connection. It could be initialized like this:">
 | 
				
			||||||
 | 
					        <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="development.html" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    <link rel="prev" href="advanced.html" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    </head>
 | 
				
			||||||
 | 
					    <body>
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					<div class="book"
 | 
				
			||||||
 | 
					    data-level="6"
 | 
				
			||||||
 | 
					    data-chapter-title="Development"
 | 
				
			||||||
 | 
					    data-filepath="development.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 " 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 active" 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="" >Development</a>
 | 
				
			||||||
 | 
					    </h1>
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <div class="page-wrapper" tabindex="-1" role="main">
 | 
				
			||||||
 | 
					                        <div class="page-inner">
 | 
				
			||||||
 | 
					                            <section class="normal markdown-section">
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            <h1 id="gorm-development">Gorm Development</h1>
 | 
				
			||||||
 | 
					<!-- toc -->
 | 
				
			||||||
 | 
					<ul>
 | 
				
			||||||
 | 
					<li><a href="#architecture">Architecture</a></li>
 | 
				
			||||||
 | 
					<li><a href="#callbacks">Callbacks</a><ul>
 | 
				
			||||||
 | 
					<li><a href="#register-a-new-callback">Register a new callback</a></li>
 | 
				
			||||||
 | 
					<li><a href="#delete-an-existing-callback">Delete an existing callback</a></li>
 | 
				
			||||||
 | 
					<li><a href="#replace-an-existing-callback">Replace an existing callback</a></li>
 | 
				
			||||||
 | 
					<li><a href="#register-callback-orders">Register callback orders</a></li>
 | 
				
			||||||
 | 
					<li><a href="#callback-api">Callback API</a></li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					</li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					<!-- toc stop -->
 | 
				
			||||||
 | 
					<h2 id="architecture">Architecture</h2>
 | 
				
			||||||
 | 
					<p>The most notable component of Gorm is<code>gorm.DB</code>, which hold database connection. It could be initialized like this:</p>
 | 
				
			||||||
 | 
					<pre><code>db, err := gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
 | 
				
			||||||
 | 
					</code></pre><p>Gorm has chainable API, <code>gorm.DB</code> is the bridge of chains, it save related information and pass it to the next chain.</p>
 | 
				
			||||||
 | 
					<p>Lets use below code to explain how it works:</p>
 | 
				
			||||||
 | 
					<pre><code>db.Where("name = ?", "jinzhu").Find(&users)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// equivalent code
 | 
				
			||||||
 | 
					newdb := db.Where("name =?", "jinzhu")
 | 
				
			||||||
 | 
					newdb.Find(&user)
 | 
				
			||||||
 | 
					</code></pre><p><code>newdb</code> is <code>db</code>'s clone, in addition, it contains search conditions from the <code>Where</code> method.
 | 
				
			||||||
 | 
					<code>Find</code> is a query method, it creates a <code>Scope</code> instance, and pass it as argument to query callbacks.</p>
 | 
				
			||||||
 | 
					<p>There are four kinds of callbacks corresponds to sql's CURD: create callbacks, update callbacks, query callbacks, delete callbacks.</p>
 | 
				
			||||||
 | 
					<h2 id="callbacks">Callbacks</h2>
 | 
				
			||||||
 | 
					<h3 id="register-a-new-callback">Register a new callback</h3>
 | 
				
			||||||
 | 
					<pre><code>func updateCreated(scope *Scope) {
 | 
				
			||||||
 | 
					    if scope.HasColumn("Created") {
 | 
				
			||||||
 | 
					        scope.SetColumn("Created", NowFunc())
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					db.Callback().Create().Register("update_created_at", updateCreated)
 | 
				
			||||||
 | 
					// register a callback for Create process
 | 
				
			||||||
 | 
					</code></pre><h3 id="delete-an-existing-callback">Delete an existing callback</h3>
 | 
				
			||||||
 | 
					<pre><code>db.Callback().Create().Remove("gorm:create")
 | 
				
			||||||
 | 
					// delete callback `gorm:create` from Create callbacks
 | 
				
			||||||
 | 
					</code></pre><h3 id="replace-an-existing-callback">Replace an existing callback</h3>
 | 
				
			||||||
 | 
					<pre><code>db.Callback().Create().Replace("gorm:create", newCreateFunction)
 | 
				
			||||||
 | 
					// replace callback `gorm:create` with new function `newCreateFunction` for Create process
 | 
				
			||||||
 | 
					</code></pre><h3 id="register-callback-orders">Register callback orders</h3>
 | 
				
			||||||
 | 
					<pre><code>db.Callback().Create().Before("gorm:create").Register("update_created_at", updateCreated)
 | 
				
			||||||
 | 
					db.Callback().Create().After("gorm:create").Register("update_created_at", updateCreated)
 | 
				
			||||||
 | 
					db.Callback().Query().After("gorm:query").Register("my_plugin:after_query", afterQuery)
 | 
				
			||||||
 | 
					db.Callback().Delete().After("gorm:delete").Register("my_plugin:after_delete", afterDelete)
 | 
				
			||||||
 | 
					db.Callback().Update().Before("gorm:update").Register("my_plugin:before_update", beforeUpdate)
 | 
				
			||||||
 | 
					db.Callback().Create().Before("gorm:create").After("gorm:before_create").Register("my_plugin:before_create", beforeCreate)
 | 
				
			||||||
 | 
					</code></pre><h3 id="callback-api">Callback API</h3>
 | 
				
			||||||
 | 
					<p>Gorm is powered by callbacks, so you could refer below links to learn how to write callbacks</p>
 | 
				
			||||||
 | 
					<p><a href="https://github.com/jinzhu/gorm/blob/master/callback_create.go" target="_blank">Create callbacks</a></p>
 | 
				
			||||||
 | 
					<p><a href="https://github.com/jinzhu/gorm/blob/master/callback_update.go" target="_blank">Update callbacks</a></p>
 | 
				
			||||||
 | 
					<p><a href="https://github.com/jinzhu/gorm/blob/master/callback_query.go" target="_blank">Query callbacks</a></p>
 | 
				
			||||||
 | 
					<p><a href="https://github.com/jinzhu/gorm/blob/master/callback_delete.go" target="_blank">Delete callbacks</a></p>
 | 
				
			||||||
 | 
					<p>View <a href="https://github.com/jinzhu/gorm/blob/master/scope.go" target="_blank">https://github.com/jinzhu/gorm/blob/master/scope.go</a> for all available API</p>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            </section>
 | 
				
			||||||
 | 
					                        </div>
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="advanced.html" class="navigation navigation-prev " aria-label="Previous page: Overriding Logger">
 | 
				
			||||||
 | 
					                    <i class="fa fa-angle-left"></i>
 | 
				
			||||||
 | 
					                </a>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="development.html" class="navigation navigation-next " aria-label="Next page: Write Plugins">
 | 
				
			||||||
 | 
					                    <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>
 | 
				
			||||||
							
								
								
									
										1
									
								
								gitbook
									
									
									
									
									
										Submodule
									
								
							
							
								
								
								
								
								
								
									
									
								
							
						
						
									
										1
									
								
								gitbook
									
									
									
									
									
										Submodule
									
								
							@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					Subproject commit 294951f22af41edbbe758fdcd0903195a332b524
 | 
				
			||||||
							
								
								
									
										704
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										704
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,704 @@
 | 
				
			|||||||
 | 
					<!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>
 | 
				
			||||||
							
								
								
									
										
											BIN
										
									
								
								logger.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								logger.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 65 KiB  | 
							
								
								
									
										747
									
								
								models.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										747
									
								
								models.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,747 @@
 | 
				
			|||||||
 | 
					<!DOCTYPE HTML>
 | 
				
			||||||
 | 
					<html lang="" >
 | 
				
			||||||
 | 
					    <head>
 | 
				
			||||||
 | 
					        <title>Models · 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="Gorm has defined struct gorm.Model, which could be embeded in your models, it will add fields ID, CreatedAt, UpdatedAt, DeletedAt to your model">
 | 
				
			||||||
 | 
					        <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="models.html" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    <link rel="prev" href="database.html" />
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    </head>
 | 
				
			||||||
 | 
					    <body>
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					<div class="book"
 | 
				
			||||||
 | 
					    data-level="2"
 | 
				
			||||||
 | 
					    data-chapter-title="Models"
 | 
				
			||||||
 | 
					    data-filepath="models.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 " 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 active" 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="" >Models</a>
 | 
				
			||||||
 | 
					    </h1>
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <div class="page-wrapper" tabindex="-1" role="main">
 | 
				
			||||||
 | 
					                        <div class="page-inner">
 | 
				
			||||||
 | 
					                            <section class="normal markdown-section">
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            <h1 id="models">Models</h1>
 | 
				
			||||||
 | 
					<!-- toc -->
 | 
				
			||||||
 | 
					<ul>
 | 
				
			||||||
 | 
					<li><a href="#model-defination">Model Defination</a></li>
 | 
				
			||||||
 | 
					<li><a href="#conventions-overriding-conventions">Conventions & Overriding Conventions</a><ul>
 | 
				
			||||||
 | 
					<li><a href="#gormmodel-struct"><code>gorm.Model</code> struct</a></li>
 | 
				
			||||||
 | 
					<li><a href="#table-name-is-the-pluralized-version-of-struct-name">Table name is the pluralized version of struct name</a></li>
 | 
				
			||||||
 | 
					<li><a href="#column-name-is-the-snake-case-of-fields-name">Column name is the snake case of field's name</a></li>
 | 
				
			||||||
 | 
					<li><a href="#field-id-as-primary-key">Field <code>ID</code> as primary key</a></li>
 | 
				
			||||||
 | 
					<li><a href="#field-createdat-used-to-store-records-created-time">Field <code>CreatedAt</code> used to store record's created time</a></li>
 | 
				
			||||||
 | 
					<li><a href="#use-updatedat-used-to-store-records-updated-time">Use <code>UpdatedAt</code> used to store record's updated time</a></li>
 | 
				
			||||||
 | 
					<li><a href="#use-deletedat-to-store-records-deleted-time-if-field-exists">Use <code>DeletedAt</code> to store record's deleted time if field exists</a></li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					</li>
 | 
				
			||||||
 | 
					</ul>
 | 
				
			||||||
 | 
					<!-- toc stop -->
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					  gorm<span class="token operator">.</span>Model
 | 
				
			||||||
 | 
					    Birthday     time<span class="token operator">.</span>Time
 | 
				
			||||||
 | 
					    Age          <span class="token builtin">int</span>
 | 
				
			||||||
 | 
					    Name         <span class="token builtin">string</span>  <span class="token string">`sql:"size:255"`</span> <span class="token comment" spellcheck="true">// 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:"AUTO_INCREMENT"`</span>
 | 
				
			||||||
 | 
					    IgnoreMe     <span class="token builtin">int</span> <span class="token string">`sql:"-"`</span>   <span class="token comment" spellcheck="true">// Ignore this field</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h2 id="conventions--overriding-conventions">Conventions & Overriding Conventions</h2>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					<pre><code class="lang-go"><span class="token comment" spellcheck="true">// Model's definition</span>
 | 
				
			||||||
 | 
					<span class="token keyword">type</span> Model <span class="token keyword">struct</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					  ID        <span class="token builtin">uint</span> <span class="token string">`gorm:"primary_key"`</span>
 | 
				
			||||||
 | 
					  CreatedAt time<span class="token operator">.</span>Time
 | 
				
			||||||
 | 
					  UpdatedAt time<span class="token operator">.</span>Time
 | 
				
			||||||
 | 
					  DeletedAt <span class="token operator">*</span>time<span class="token operator">.</span>Time
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// set User'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="token keyword">return</span> <span class="token string">"profiles"</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="token keyword">if</span> u<span class="token operator">.</span>Role <span class="token operator">==</span> <span class="token string">"admin"</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					        <span class="token keyword">return</span> <span class="token string">"admin_users"</span>
 | 
				
			||||||
 | 
					    <span class="token operator">}</span> <span class="token keyword">else</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					        <span class="token keyword">return</span> <span class="token string">"users"</span>
 | 
				
			||||||
 | 
					    <span class="token operator">}</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// Disable table name'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`'s default table name will be `user`, table name setted with `TableName` won't be affected</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h3 id="column-name-is-the-snake-case-of-fields-name">Column name is the snake case of field'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>
 | 
				
			||||||
 | 
					  ID <span class="token builtin">uint</span>             <span class="token comment" spellcheck="true">// 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>
 | 
				
			||||||
 | 
					  Birthday time<span class="token operator">.</span>Time  <span class="token comment" spellcheck="true">// 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>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token keyword">type</span> Animal <span class="token keyword">struct</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					    AnimalId    <span class="token builtin">int64</span>     <span class="token string">`gorm:"column:beast_id"`</span>         <span class="token comment" spellcheck="true">// set column name to `beast_id`</span>
 | 
				
			||||||
 | 
					    Birthday    time<span class="token operator">.</span>Time <span class="token string">`gorm:"column:day_of_the_beast"`</span> <span class="token comment" spellcheck="true">// set column name to `day_of_the_beast`</span>
 | 
				
			||||||
 | 
					    Age         <span class="token builtin">int64</span>     <span class="token string">`gorm:"column:age_of_the_beast"`</span> <span class="token comment" spellcheck="true">// set column name to `age_of_the_beast`</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<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>
 | 
				
			||||||
 | 
					  ID <span class="token builtin">uint</span>  <span class="token comment" spellcheck="true">// field named `ID` is the default primary key for `User`</span>
 | 
				
			||||||
 | 
					  Name <span class="token builtin">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="token keyword">type</span> Animal <span class="token keyword">struct</span> <span class="token operator">{</span>
 | 
				
			||||||
 | 
					  AnimalId <span class="token builtin">int64</span> <span class="token string">`gorm:"primary_key"`</span> <span class="token comment" spellcheck="true">// set AnimalId to be primary key</span>
 | 
				
			||||||
 | 
					  Name     <span class="token builtin">string</span>
 | 
				
			||||||
 | 
					  Age      <span class="token builtin">int64</span>
 | 
				
			||||||
 | 
					<span class="token operator">}</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h3 id="field-createdat-used-to-store-records-created-time">Field <code>CreatedAt</code> used to store record's created time</h3>
 | 
				
			||||||
 | 
					<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">&</span>user<span class="token operator">)</span> <span class="token comment" spellcheck="true">// will set `CreatedAt` to current time</span>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<span class="token comment" spellcheck="true">// 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">&</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">"CreatedAt"</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>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h3 id="use-updatedat-used-to-store-records-updated-time">Use <code>UpdatedAt</code> used to store record's updated time</h3>
 | 
				
			||||||
 | 
					<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">&</span>user<span class="token operator">)</span> <span class="token comment" spellcheck="true">// 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">&</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">"name"</span><span class="token operator">,</span> <span class="token string">"jinzhu"</span><span class="token operator">)</span> <span class="token comment" spellcheck="true">// will set `UpdatedAt` to current time</span>
 | 
				
			||||||
 | 
					</code></pre>
 | 
				
			||||||
 | 
					<h3 id="use-deletedat-to-store-records-deleted-time-if-field-exists">Use <code>DeletedAt</code> to store record's deleted time if field exists</h3>
 | 
				
			||||||
 | 
					<p>Delete records having <code>DeletedAt</code> field, it won't delete the record from database, but will set field <code>DeletedAt</code>'s value to current time.</p>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                            </section>
 | 
				
			||||||
 | 
					                        </div>
 | 
				
			||||||
 | 
					                    </div>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="database.html" class="navigation navigation-prev " aria-label="Previous page: Migration">
 | 
				
			||||||
 | 
					                    <i class="fa fa-angle-left"></i>
 | 
				
			||||||
 | 
					                </a>
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                <a href="models.html" class="navigation navigation-next " aria-label="Next page: Model Defination">
 | 
				
			||||||
 | 
					                    <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>
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user