title: Modeling a prescription/medicine usage set of database tables
body: <p>Not sure how to go about modelling medication taken at a point in time in a relational database. Each way I have thought of so far seems to have redundant data.</p>
  
  <p>Need to be able to have a user say I took x tablets or y mg of some drug. </p>
  
  <p>Currently I have a Diary table, which has a One to Many relationship with Entries. Each entry has description, date, time etc, which are all fine. But I want to be able to have the amount of medication taken. </p>
  
  <p>Current thinking is to have a Drug table, which has a recursive many to many relationship with itself, so that one Drug can contain multiple other drugs, or itself may be the active ingredient. </p>
  
  <p>So for the drug 'codeine' the active ingredient is 'codeine' (not actually the active ingredient, but illustrates the point)</p>
  
  <p>But you also have drugs such as cocodamol, which contains codeine and paracetamol. So has to map to both the 'codeine' row and the 'paracetamol' row.</p>
  
  <p>How would I be able to have a user with a diary, add an entry to say:</p>
  
  <p>I took 2 cocodamol of 30mg codeine and 500mg paracetamol strength?</p>
  
  <p>As a raw data structure it would be something like (ignoring all database parts):</p>
  
  <pre><code>{'diary': 
      {'entry': 
          {'date': '2014-11-11', 
           'medication': {
               'cocodamol': {
                   'codeine': 60, 
                   'paracetamol': 1000
               }
           }
       }
   }
  </code></pre>
  
  <p>The reason for this being a table and not just flat text field, is </p>
  
  <pre><code>a) I want to create statistics for medication taken
  b) I want the drug table to stay generic so I can link information to that drug
  c) Would be useful to store prescription data as well as diary entries, so a Dr has prescribed taking 2 cocodamol 30/500mg up to every four hours
  </code></pre>
  
  <p>Which begs the question where would weight go, and how is the data modeled in general.</p>
  
  <p>Have drawn up a relation diagram of what i <em>think</em> it should be like, but i'm still unclear as to how i would have a dose of 30/500mg of cocodamol, the following should take care of any single dose, whatever the unit of measurement may be. </p>
  
  <p><img src="https://i.stack.imgur.com/DVLnR.jpg" alt="entity relation diagram of diary -&gt; drugs"></p>
  
  <p>Would it be best to just have the input as {cocodamol: 30/500mg} then on the backend have two doses, one 30mg codeine and then one 500mg paracetamol?</p>
  
  <p>This would probably mean linking Dosage to the ActiveIngredient table instead of Drug, and then using the reverse of that link to determine what the user took. So if we know they had a dose of 500mg paracetamol and 30mg codeine, which are both linked to the Drug cocodamol, we can recreate their medication taken instead of its constituent parts.</p>
  
  <p>Cheers,</p>
  
  <p>Brett </p>
  
  <p>(there are fields missing from some of those tables, which only serve to cloud the issue (i think))</p>