title: Identifying the rows of my table with 'type' column
body: <p>Let's say for example i have 3 tables:</p>
  
  <p>Users - Images - Product.</p>
  
  <p><a href="https://i.stack.imgur.com/Od4jK.png" rel="nofollow noreferrer">Tables</a></p>
  
  <p>It looks like that. How do i easily identify whether the row is for the user or for the product. </p>
  
  <p>My first solution was to name a single (ID) inside image/cover table and have it reference to 2 tables and add a <code>'type'</code> column with <strong>string data type</strong> and just store <code>'user'</code> and/or <code>'product'</code> strings inside it to determine if the <code>'id'</code> is a <code>user_id</code> or a <code>product_id</code>. </p>
  
  <p>Aside from the obvious mistake of referencing 2 tables to one foreign key. Is there any other problem with my idea? </p>
  
  <p>Maybe i can just add two (ID). Namely, <code>user_id</code> and <code>product_id</code> and declare both as NULL. I would be able to determine which table the row belongs to.</p>
  
  <p>if <code>user_id</code> has a value of <strong>'1'</strong> and <code>product_id</code> is <strong>NULL</strong> then that row belongs to user.</p>
  
  <p>Sample controller/model code snippet for inserting a user cover/image.</p>
  
  <p>Controller:</p>
  
  <pre><code>$insert['user_id'] = $this-&gt;user_id;
  $insert['product_id'] = NULL;
  $insert['original'] = $image_name;
  $insert['thumbnail'] = $this-&gt;upload-&gt;data('raw_name').'_thumb';
  
  $image_id = $this-&gt;model-&gt;insert_cover($insert);
  
  if($image_id &gt; 0){//do something }
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>public function insert_cover($data){
  $query = $this-&gt;db-&gt;insert('cover/image', $data);
  if($query){ return $this-&gt;db-&gt;insert_id(); }else{ return 0; }
  }
  </code></pre>
  
  <p>I have default requirements for uploading an image to my project folder <code>'/assets/uploaded_images/'</code> but i want to display all product images as <strong>200x200</strong> and all user images as <strong>100x100</strong> so i used image_lib in codeigniter to resize the original uploaded image and the resized image is automatically named with a suffix of <code>'_thumb'</code>.</p>
  
  <p>I then store the file name of both the original and the thumbnail to my database and when displaying them on my view i just add a url string </p>
  
  <p><code>$cover = 'http://localhost/project_name/assets/uploaded_images/'.$file_name</code></p>
  
  <p>Anyway, so what is the best method can i apply here that will easily identify which table the row belongs to? </p>
  
  <p>Do i opt for the 2 id with NULL values? do i add a 'type' column that is a string?</p>
  
  <p>But why do i feel its tacky/loose? whenever i insert something i would have to add:</p>
  
  <pre><code>$insert['type'] = 'user';
  </code></pre>
  
  <p>I think its because i have no control over 'type' column. </p>
  
  <p>What if i add another table named <code>image_type</code> or <code>cover_type</code> with 2 columns. Namely, <code>type_id</code> and <code>type_name</code>.</p>
  
  <p>Then whenever i insert into the image table i would have to query for the <code>'type'</code> column first.</p>
  
  <pre><code>//inserting user image/cover
  
      $type = $this-&gt;model-&gt;check_type('user');
  
      if($type != NULL){ 
      // do something 
      }
  
      public function check_Type($type){
      $this-&gt;db-&gt;select('type_id');
      $query = $this-&gt;db-&gt;get_where('image_type', array('type_name' = $type));
      if($query-&gt;num_rows() &gt; 0){
      foreach($query-&gt;result() as $row){
      $type = $row-&gt;type_id;}
      return $type;
      }else{ return NULL; }
  </code></pre>