title: Is this a good idea to store relations to many different tables in one field?
body: <p>I have 4 basic models which are stored in following tables:</p>
  
  <ul>
  <li>users</li>
  <li>images</li>
  <li>videos</li>
  <li>posts</li>
  </ul>
  
  <p>In application logic user may "love" <code>image</code> or <code>url</code> or <code>post</code> (for example user clicks little heart next to image or video etc). So I have to keep all "loves" in database. My idea is to use table <code>loves</code> with following columns:</p>
  
  <pre><code>id - love id, primary_key
  user_id - id of user who loves something, foreign_key
  entity_type - type of entity which is loved by user, enum('image', 'video', 'post')
  entity_id - id of entity which is loves (either image_id, video_id, post_id)
  
  unique index on (user_id,entity_type,entity_id) -&gt; user can love unique entity only once
  </code></pre>
  
  <p>In this solution I can't declare <code>foreign_key</code> on <code>loves.entity_id</code> because I dont know which table it refers to...</p>
  
  <p>Is this a good approach? </p>
  
  <p>Is there better solution than this? </p>
  
  <p>I am using <code>postgres</code> but I tagged question with <code>mysql</code> because maybe is general problem in relational dbs?</p>