title: MySQL - Conditional Foreign Key Constraints
body: <p>I have following <code>comments</code> table in my app:</p>
  
  <pre><code>comments
  --------
  id           INT
  foreign_id   INT
  model        TEXT
  comment_text TEXT
  ...
  </code></pre>
  
  <p>the idea of this table is to store comments for various parts of my app - it can store comments for blog post i.e.:</p>
  
  <pre><code>1|34|blogpost|lorem ipsum...
  </code></pre>
  
  <p>user picture:</p>
  
  <pre><code>2|12|picture|lorem ipsum...
  </code></pre>
  
  <p>and so on.</p>
  
  <p>now, is there a way to force FOREIGN KEY constraint on such data?</p>
  
  <p>i.e. something like this in comments table:</p>
  
  <pre><code>FOREIGN KEY (`foreign_id`) REFERENCES blogposts (`id`)
  -- but only when model='blogpost'
  </code></pre>