github.com/System-Glitch/goyave/v2@v2.10.3-0.20200819142921-51011e75d504/docs/guide/basics/database.html (about)

     1  <!DOCTYPE html>
     2  <html lang="en-US">
     3    <head>
     4      <meta charset="utf-8">
     5      <meta name="viewport" content="width=device-width,initial-scale=1">
     6      <title>Database | Goyave</title>
     7      <meta name="generator" content="VuePress 1.5.3">
     8      <link rel="icon" type="image/png" sizes="16x16" href="/goyave/goyave_16.png">
     9      <link rel="icon" type="image/png" sizes="32x32" href="/goyave/goyave_32.png">
    10      <link rel="icon" type="image/png" sizes="64x64" href="/goyave/goyave_64.png">
    11      <link rel="icon" type="image/png" sizes="128x128" href="/goyave/goyave_128.png">
    12      <link rel="icon" type="image/png" sizes="256x256" href="/goyave/goyave_256.png">
    13      <link rel="icon" type="image/png" sizes="512x512" href="/goyave/goyave_512.png">
    14      <meta name="description" content="Goyave is a Golang web API framework aiming at cleanliness, fast development and power.">
    15      <meta name="og:title" content="Database - Goyave">
    16      <meta name="twitter:title" content="Database - Goyave">
    17      <meta name="title" content="Database - Goyave">
    18      <meta property="twitter:description" content="Goyave is a Golang web API framework aiming at cleanliness, fast development and power.">
    19      <meta property="twitter:image:src" content="https://system-glitch.github.io/goyave/goyave_banner.png">
    20      <meta property="twitter:card" content="summary_large_image">
    21      <meta property="og:type" content="website">
    22      <meta property="og:description" content="Goyave is a Golang web API framework aiming at cleanliness, fast development and power.">
    23      <meta property="og:image" content="https://system-glitch.github.io/goyave/goyave_banner.png">
    24      <meta property="og:site_name" content="Goyave">
    25      <link rel="preload" href="/goyave/assets/css/0.styles.589fd562.css" as="style"><link rel="preload" href="/goyave/assets/js/app.092490a7.js" as="script"><link rel="preload" href="/goyave/assets/js/4.75a9cc68.js" as="script"><link rel="preload" href="/goyave/assets/js/1.121dd9ed.js" as="script"><link rel="preload" href="/goyave/assets/js/18.470b55ed.js" as="script"><link rel="preload" href="/goyave/assets/js/5.c83f1192.js" as="script"><link rel="prefetch" href="/goyave/assets/js/10.2f07bbf5.js"><link rel="prefetch" href="/goyave/assets/js/11.2d66fdef.js"><link rel="prefetch" href="/goyave/assets/js/12.63171b15.js"><link rel="prefetch" href="/goyave/assets/js/13.770050f3.js"><link rel="prefetch" href="/goyave/assets/js/14.b933d8cf.js"><link rel="prefetch" href="/goyave/assets/js/15.36df2a66.js"><link rel="prefetch" href="/goyave/assets/js/16.ed66719e.js"><link rel="prefetch" href="/goyave/assets/js/17.7bef5f05.js"><link rel="prefetch" href="/goyave/assets/js/19.90e0dab8.js"><link rel="prefetch" href="/goyave/assets/js/20.3a300ca3.js"><link rel="prefetch" href="/goyave/assets/js/21.c3fd6053.js"><link rel="prefetch" href="/goyave/assets/js/22.d5569617.js"><link rel="prefetch" href="/goyave/assets/js/23.931b2034.js"><link rel="prefetch" href="/goyave/assets/js/24.1a4755e7.js"><link rel="prefetch" href="/goyave/assets/js/25.0d463913.js"><link rel="prefetch" href="/goyave/assets/js/26.3c173a7a.js"><link rel="prefetch" href="/goyave/assets/js/27.9c5b36f2.js"><link rel="prefetch" href="/goyave/assets/js/28.41e055b7.js"><link rel="prefetch" href="/goyave/assets/js/29.b87adf4a.js"><link rel="prefetch" href="/goyave/assets/js/3.ef71e77d.js"><link rel="prefetch" href="/goyave/assets/js/6.2336bf0c.js"><link rel="prefetch" href="/goyave/assets/js/7.d60e55c1.js"><link rel="prefetch" href="/goyave/assets/js/8.2ee33a42.js"><link rel="prefetch" href="/goyave/assets/js/9.8e043d60.js">
    26      <link rel="stylesheet" href="/goyave/assets/css/0.styles.589fd562.css">
    27    </head>
    28    <body>
    29      <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/goyave/" class="home-link router-link-active"><img src="/goyave/goyave_64.png" alt="Goyave" class="logo"> <span class="site-name can-hide">Goyave</span></a> <div class="links"><div class="user-settings"><a title="Dark theme" href="#" class="settings-button"><svg aria-hidden="true" data-prefix="fas" data-icon="cog" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="svg-inline--fa fa-cog fa-w-16 settings-icon"><path fill="currentColor" d="M8 0c-4.4 0-8 3.6-8 8s3.6 8 8 8 8-3.6 8-8-3.6-8-8-8zM8 15c-3.9 0-7-3.1-7-7 0-2.4 1.2-4.6 3.2-5.9-0.1 0.6-0.2 1.3-0.2 1.9 0 4.9 4 8.9 8.9 9-1.3 1.3-3 2-4.9 2z"></path></svg></a></div> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/goyave/guide/" class="nav-link router-link-active">
    30    Guide
    31  </a></div><div class="nav-item"><a href="https://pkg.go.dev/github.com/System-Glitch/goyave/v2" target="_blank" rel="noopener noreferrer" class="nav-link external">
    32    pkg.go.dev
    33    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <a href="https://github.com/System-Glitch/goyave" target="_blank" rel="noopener noreferrer" class="repo-link">
    34      GitHub
    35      <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/goyave/guide/" class="nav-link router-link-active">
    36    Guide
    37  </a></div><div class="nav-item"><a href="https://pkg.go.dev/github.com/System-Glitch/goyave/v2" target="_blank" rel="noopener noreferrer" class="nav-link external">
    38    pkg.go.dev
    39    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <a href="https://github.com/System-Glitch/goyave" target="_blank" rel="noopener noreferrer" class="repo-link">
    40      GitHub
    41      <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Guide</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>The Basics</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/goyave/guide/basics/routing.html" class="sidebar-link">Routing</a></li><li><a href="/goyave/guide/basics/middleware.html" class="sidebar-link">Middleware</a></li><li><a href="/goyave/guide/basics/requests.html" class="sidebar-link">Requests</a></li><li><a href="/goyave/guide/basics/controllers.html" class="sidebar-link">Controllers</a></li><li><a href="/goyave/guide/basics/responses.html" class="sidebar-link">Responses</a></li><li><a href="/goyave/guide/basics/database.html" aria-current="page" class="active sidebar-link">Database</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/goyave/guide/basics/database.html#introduction" class="sidebar-link">Introduction</a></li><li class="sidebar-sub-header"><a href="/goyave/guide/basics/database.html#configuration" class="sidebar-link">Configuration</a></li><li class="sidebar-sub-header"><a href="/goyave/guide/basics/database.html#getting-a-database-connection" class="sidebar-link">Getting a database connection</a></li><li class="sidebar-sub-header"><a href="/goyave/guide/basics/database.html#models" class="sidebar-link">Models</a></li></ul></li><li><a href="/goyave/guide/basics/validation.html" class="sidebar-link">Validation</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Advanced</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="database"><a href="#database" class="header-anchor">#</a> Database</h1> <p></p><div class="table-of-contents"><ul><li><a href="#introduction">Introduction</a></li><li><a href="#configuration">Configuration</a><ul><li><a href="#drivers">Drivers</a></li></ul></li><li><a href="#getting-a-database-connection">Getting a database connection</a></li><li><a href="#models">Models</a><ul><li><a href="#defining-a-model">Defining a model</a></li><li><a href="#hidden-fields">Hidden fields</a></li><li><a href="#automatic-migrations">Automatic migrations</a></li></ul></li></ul></div><p></p> <h2 id="introduction"><a href="#introduction" class="header-anchor">#</a> Introduction</h2> <p>Most web applications use a database. In this section, we are going to see how Goyave applications can query a database, using the awesome <a href="https://gorm.io/" target="_blank" rel="noopener noreferrer">Gorm ORM<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>.</p> <p>Database connections are managed by the framework and are long-lived. When the server shuts down, the database connections are closed automatically. So you don't have to worry about creating, closing or refreshing database connections in your application.</p> <p>All functions below require the <code>database</code> and the <code>gorm</code> packages to be imported.</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
    42    <span class="token string">&quot;github.com/System-Glitch/goyave/v2/database&quot;</span>
    43    <span class="token string">&quot;github.com/jinzhu/gorm&quot;</span>
    44  <span class="token punctuation">)</span>
    45  </code></pre></div><h2 id="configuration"><a href="#configuration" class="header-anchor">#</a> Configuration</h2> <p>Very few code is required to get started with databases. There are some <a href="/goyave/guide/configuration.html#database-category">configuration</a> options that you need to change though:</p> <ul><li><code>database.connection</code></li> <li><code>database.host</code></li> <li><code>database.port</code></li> <li><code>database.name</code></li> <li><code>database.username</code></li> <li><code>database.password</code></li> <li><code>database.options</code></li> <li><code>database.maxOpenConnection</code></li> <li><code>database.maxIdleConnection</code></li> <li><code>database.maxLifetime</code></li></ul> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p><code>database.options</code> represents the additional connection options. For example, when using MySQL, you should use the <code>parseTime=true</code> option so <code>time.Time</code> can be handled correctly. Available options differ from one driver to another and can be found in their respective documentation.</p></div> <h3 id="drivers"><a href="#drivers" class="header-anchor">#</a> Drivers</h3> <p>The framework supports the following sql drivers:</p> <ul><li><code>none</code> (<em>Disable database features</em>)</li> <li><code>mysql</code></li> <li><code>postgres</code></li> <li><code>sqlite3</code></li> <li><code>mssql</code></li></ul> <p>Change the <code>database.connection</code> config entry to the desired driver.</p> <p>In order to be able connect to the database, Gorm needs a database driver to be imported. Add the following import to your <code>kernel.go</code>:</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token boolean">_</span> <span class="token string">&quot;github.com/jinzhu/gorm/dialects/mysql&quot;</span>
    46  <span class="token comment">// import _ &quot;github.com/jinzhu/gorm/dialects/postgres&quot;</span>
    47  <span class="token comment">// import _ &quot;github.com/jinzhu/gorm/dialects/sqlite&quot;</span>
    48  <span class="token comment">// import _ &quot;github.com/jinzhu/gorm/dialects/mssql&quot;</span>
    49  </code></pre></div><div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>For SQLite, only the <code>database.name</code> config entry is required.</p></div> <h2 id="getting-a-database-connection"><a href="#getting-a-database-connection" class="header-anchor">#</a> Getting a database connection</h2> <h4 id="database-getconnection"><a href="#database-getconnection" class="header-anchor">#</a> database.GetConnection</h4> <p>Returns the global database connection pool. Creates a new connection pool if no connection is available.</p> <p>The connections will be closed automatically on server shutdown so you don't need to call <code>Close()</code> when you're done with the database.</p> <table><thead><tr><th>Parameters</th> <th>Return</th></tr></thead> <tbody><tr><td></td> <td><code>*gorm.DB</code></td></tr></tbody></table> <p><strong>Example:</strong></p> <div class="language-go extra-class"><pre class="language-go"><code>db <span class="token operator">:=</span> database<span class="token punctuation">.</span><span class="token function">GetConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    50  db<span class="token punctuation">.</span><span class="token function">First</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>
    51  </code></pre></div><div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>Learn how to use the CRUD interface and the query builder in the <a href="https://gorm.io/docs/index.html" target="_blank" rel="noopener noreferrer">Gorm documentation<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>.</p></div> <h4 id="database-close"><a href="#database-close" class="header-anchor">#</a> database.Close</h4> <p>If you want to manually close the database connection, you can do it using <code>Close()</code>. New connections can be re-opened using <code>GetConnection()</code> as usual. This function does nothing if the database connection is already closed or has never been created.</p> <table><thead><tr><th>Parameters</th> <th>Return</th></tr></thead> <tbody><tr><td></td> <td><code>void</code></td></tr></tbody></table> <p><strong>Example:</strong></p> <div class="language-go extra-class"><pre class="language-go"><code>database<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    52  </code></pre></div><h2 id="models"><a href="#models" class="header-anchor">#</a> Models</h2> <p>A model is a structure reflecting a database table structure. An instance of a model is a single database record. Each model is defined in its own file inside the <code>database/model</code> directory.</p> <h3 id="defining-a-model"><a href="#defining-a-model" class="header-anchor">#</a> Defining a model</h3> <p>Models are usually just normal Golang structs, basic Go types, or pointers of them. <code>sql.Scanner</code> and <code>driver.Valuer</code> interfaces are also supported.</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    53      database<span class="token punctuation">.</span><span class="token function">RegisterModel</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
    54  <span class="token punctuation">}</span>
    55  
    56  <span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
    57      gorm<span class="token punctuation">.</span>Model
    58      Name         <span class="token builtin">string</span>
    59      Age          sql<span class="token punctuation">.</span>NullInt64
    60      Birthday     <span class="token operator">*</span>time<span class="token punctuation">.</span>Time
    61      Email        <span class="token builtin">string</span>  <span class="token string">`gorm:&quot;type:varchar(100);unique_index&quot;`</span>
    62      Role         <span class="token builtin">string</span>  <span class="token string">`gorm:&quot;size:255&quot;`</span> <span class="token comment">// set field size to 255</span>
    63      MemberNumber <span class="token operator">*</span><span class="token builtin">string</span> <span class="token string">`gorm:&quot;unique;not null&quot;`</span> <span class="token comment">// set member number to unique and not null</span>
    64      Num          <span class="token builtin">int</span>     <span class="token string">`gorm:&quot;AUTO_INCREMENT&quot;`</span> <span class="token comment">// set num to auto incrementable</span>
    65      Address      <span class="token builtin">string</span>  <span class="token string">`gorm:&quot;index:addr&quot;`</span> <span class="token comment">// create index with name `addr` for address</span>
    66      IgnoreMe     <span class="token builtin">int</span>     <span class="token string">`gorm:&quot;-&quot;`</span> <span class="token comment">// ignore this field</span>
    67  <span class="token punctuation">}</span>
    68  </code></pre></div><div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>All models should be <strong>registered</strong> in an <code>init()</code> function inside their model file. To ensure the <code>init()</code> functions are executed before the server starts, import the <code>models</code> package in your <code>kernel.go</code>.</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token boolean">_</span> <span class="token string">&quot;database/model&quot;</span>
    69  </code></pre></div></div> <p>Learn more about model declaration in the <a href="https://gorm.io/docs/models.html" target="_blank" rel="noopener noreferrer">Gorm documentation<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>.</p> <h4 id="database-registermodel"><a href="#database-registermodel" class="header-anchor">#</a> database.RegisterModel</h4> <p>Registers a model for auto-migration.</p> <table><thead><tr><th>Parameters</th> <th>Return</th></tr></thead> <tbody><tr><td><code>model interface{}</code></td> <td><code>void</code></td></tr></tbody></table> <h4 id="database-getregisteredmodels"><a href="#database-getregisteredmodels" class="header-anchor">#</a> database.GetRegisteredModels</h4> <p>Get the registered models. The returned slice is a copy of the original, so it cannot be modified.</p> <table><thead><tr><th>Parameters</th> <th>Return</th></tr></thead> <tbody><tr><td></td> <td><code>[]interface{}</code></td></tr></tbody></table> <h4 id="database-clearregisteredmodels"><a href="#database-clearregisteredmodels" class="header-anchor">#</a> database.ClearRegisteredModels</h4> <p>Unregister all models.</p> <table><thead><tr><th>Parameters</th> <th>Return</th></tr></thead> <tbody><tr><td></td> <td><code>void</code></td></tr></tbody></table> <h3 id="hidden-fields"><a href="#hidden-fields" class="header-anchor">#</a> Hidden fields</h3> <p><span class="badge tip" style="vertical-align:top;" data-v-15b7b770>Since v2.9.0</span></p> <p>Sometimes you may wish to exclude some fields from your model's JSON form, such as passwords. To do so, you can add the <code>model:&quot;hide&quot;</code> tag to the field you want to hide.</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
    70      Username <span class="token builtin">string</span>
    71      Password <span class="token builtin">string</span> <span class="token string">`model:&quot;hide&quot; json:&quot;,omitempty&quot;`</span>
    72  <span class="token punctuation">}</span>
    73  </code></pre></div><p>When a struct is sent as a response through <code>response.JSON()</code>, all its fields (including promoted fields) tagged with <code>model:&quot;hide&quot;</code> will be set to their zero value. Add the <code>json:&quot;,omitempty&quot;</code> tag to entirely remove the field from the resulting JSON string.</p> <p>You can also filter hidden fields by passing a struct to <a href="/goyave/guide/advanced/helpers.html#helper-removehiddenfields"><code>helper.RemoveHiddenFields()</code></a>.</p> <h3 id="automatic-migrations"><a href="#automatic-migrations" class="header-anchor">#</a> Automatic migrations</h3> <p>If the <code>database.autoMigrate</code> config option is set to true, all registered models will be automatically migrated when the server starts.</p> <div class="custom-block warning"><p class="custom-block-title">WARNING</p> <p>Automatic migrations <strong>only create</strong> tables, missing columns and missing indexes. They <strong>wont't change</strong> existing column’s type or delete unused columns.</p></div> <p>If you would like to know more about migrations using Gorm, read their <a href="https://gorm.io/docs/migration.html" target="_blank" rel="noopener noreferrer">documentation<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>.</p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/System-Glitch/goyave/edit/master/docs_src/src/guide/basics/database.md" target="_blank" rel="noopener noreferrer">Edit this page on GitHub</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
    74        ←
    75        <a href="/goyave/guide/basics/responses.html" class="prev">
    76          Responses
    77        </a></span> <span class="next"><a href="/goyave/guide/basics/validation.html">
    78          Validation
    79        </a>
    80        →
    81      </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    82      <script src="/goyave/assets/js/app.092490a7.js" defer></script><script src="/goyave/assets/js/4.75a9cc68.js" defer></script><script src="/goyave/assets/js/1.121dd9ed.js" defer></script><script src="/goyave/assets/js/18.470b55ed.js" defer></script><script src="/goyave/assets/js/5.c83f1192.js" defer></script>
    83    </body>
    84  </html>