title: DynamoDB adjacency list primary key
body: <p>I am completing an exercise using DynamoDB to model a many to many relationship. I need to allow a many to many relationship between posts and tags. Each post can have many tags and each tag can have many posts.</p>
  
  <p>I have a primary key on <code>id</code> and primary sort key on <code>type</code> and then another global index on <code>id</code> and <code>data</code>, I added another global index on <code>id</code> and <code>type</code> again but I think this is redundant. </p>
  
  <p>Here is what I have so far.</p>
  
  <pre><code>id(Partition key)      type(Sort Key)       target       data
  -------------          ----------           ------       ------
  1                      post                 1            cool post
  tag                    tag                  tag          n/a
  1                      tag                  tag          orange
  
  ---------------------------------------------
  ---- inserting another tag will overwrite ---
  ---------------------------------------------
  
  1                      tag                  tag          green
  </code></pre>
  
  <p>I am taking advice from this awesome talk <a href="https://www.youtube.com/watch?v=jzeKPKpucS0" rel="noreferrer">https://www.youtube.com/watch?v=jzeKPKpucS0</a> and these not so awesome docs <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-adjacency-graphs.html" rel="noreferrer">https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-adjacency-graphs.html</a></p>
  
  <p>The issue I am having is that if I try to add another tag with an <code>id</code> "1" and <code>type</code> "tag" it will overwrite the existing tag because it would have the same composite key. What am I missing here? It seems like the suggestion is to make the primary key and sort key be the <code>id</code> and <code>type</code>. Should I have my type be more like "tag#orange"? In that case I could put a global index on the <code>target</code> with a sort key on the type. This way I could get all posts with a certain tag by querying target = "tag" and type starts with "tag".</p>
  
  <p>Just looking for some advice on handling this sort of adjacency list data with Dynamo as it seems very interesting. Thanks!</p>