title: How do I conditionally insert an item into a dynamodb table using boto3
body: <p>If I have a table with a hash key of userId and a range key of productId how do I put an item into that table only if it doesn't already exist using boto3's dynamodb bindings?</p>
  
  <p>The normal call to put_item looks like this</p>
  
  <pre><code>table.put_item(Item={'userId': 1, 'productId': 2})
  </code></pre>
  
  <p>My call with a ConditionExpression looks like this:</p>
  
  <pre><code>table.put_item(
      Item={'userId': 1, 'productId': 2}, 
      ConditionExpression='userId &lt;&gt; :uid AND productId &lt;&gt; :pid', 
      ExpressionAttributeValues={':uid': 1, ':pid': 3}
  )
  </code></pre>
  
  <p>But this raises a ConditionalCheckFailedException every time. Whether an item exists with the same productId or not. </p>