title: Inserting data to multiple tables Postgres
body: <p>I currently have a MongoDB database with the following schema:</p>
  
  <pre><code>Image: { name: String, src: String, category: String, tags: [String] }
  </code></pre>
  
  <p>I'd like to migrate this to Postgres and for that I'd have 4 tables </p>
  
  <pre><code>image (id, src, name, category_id)
  tag (id, name)
  image_tag (image_id, tag_id)
  category (id, name)
  </code></pre>
  
  <p>There might be new <code>tag</code>s on every <code>image</code> inserts so when using CTE I need to select all the tags (and only insert new tags if they don't exist). I was thinking about using a cache (redis) to store the already inserted tags (so I don't need to select them from the db).</p>
  
  <p>So my question is should I go with CTE with <code>insert into tags.. where not exists</code> statements or CTE + redis and only inserting tags when it could not be found in the cache?</p>