title: How to convert a SQL Alchemy ORM to a dict
body: <p>I'm trying to convert my SQL Alchemy to a basic dict.</p>
  <p>For this case, I'm trying to use the <code>__dict__</code> builtin function.</p>
  <p>But the behavior is quite strange.</p>
  <p>Here is my function:</p>
  <pre><code>def trial_to_dict(sa_trial_instance):
      &quot;&quot;&quot;&quot;Convert a SQL Alchemy instance to a JSON dict
      params:
          sa_trial_instance (dict): SQL Alchemy instance
      returns:
          dict: JSON
      &quot;&quot;&quot;
  
      temp_dict = sa_trial_instance.__dict__
  
      return {
          &quot;ct_id&quot;: temp_dict[&quot;ct_id&quot;],
          &quot;study_type&quot;: temp_dict[&quot;study_type&quot;],
          &quot;phase&quot;: temp_dict[&quot;phase&quot;]
      }
  </code></pre>
  <p><code>sa_trial_instance</code> is defined, and it's a Sql Alchemy instance
  <a href="https://i.stack.imgur.com/j5uHr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j5uHr.png" alt="sa_trial_instance" /></a></p>
  <p>But <code>temp_dict</code> isn't the result I'm expecting :
  <a href="https://i.stack.imgur.com/CJ61n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CJ61n.png" alt="temp_dict" /></a></p>
  <p>Obvioulsy, I'm getting this error:</p>
  <pre><code>Exception has occurred: KeyError
  'ct_id'
  </code></pre>
  <p>Is anyone has an explanation about this?
  Thank you for your help!</p>