title: GORM - Golang Change Database at Runtime
body: <p>In the UI you can select different options in a dropdown, which relate to data held in different databases.  I have created models for the data, and as the table schemas are the same between the DBs, I would like to simply switch between the DBs according to which option the user has selected.</p>
  <p>How can this be achieved in GORM 2.0?  I would prefer to avoid specifying the &quot;database.tableName&quot; in the queries, as I have frequently used the</p>
  <pre><code>db.Model(&amp;model).Where...
  </code></pre>
  <p>pattern.</p>
  <p>I have figured out a method, but I was interested to see if there was a way of handling this in the gorm.DB object (or more elegantly at all!).</p>
  <p>Current solution:</p>
  <p>In the repository, before executing any database operation use the following:</p>
  <pre><code>r.db.Exec(&quot;use &quot; + utils.GetDB())
  </code></pre>
  <p>and use the API middleware to pick up any changes to the dropdown and set a global variable pertaining to the required database.</p>
  <p>Many thanks in advance.</p>