title: Checking the row existence before insertion using Cassandra within a Go application
body: <p>I am using <a href="https://github.com/gocql/gocql" rel="nofollow noreferrer">gocql</a> with my Go application and trying to solve the issue described below.</p>
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE IF NOT EXISTS website.users (
      id            uuid,
      email_address text,
      first_name    text,   
      last_name     text,   
      created_at    timestamp,
      PRIMARY KEY (email_address)
  );
  </code></pre>
  <p>This query is going to override matching record which is Cassandra's expected behaviour.</p>
  <pre class="lang-sql prettyprint-override"><code>INSERT INTO users (id, email_address, first_name, last_name, created_at)
  VALUES (?, ?, ?, ?, ?)
  </code></pre>
  <p>In order to prevent overriding the existing record we can use <code>IF NOT EXISTS</code> at the end of the query.</p>
  <pre class="lang-sql prettyprint-override"><code>INSERT INTO users (id, email_address, first_name, last_name, created_at)
  VALUES (?, ?, ?, ?, ?)
  IF NOT EXISTS
  </code></pre>
  <p>However, there is no way for me to know if the query affected any rows in DB or not. Somehow I need to return something like &quot;Record exists&quot; message back to caller but it is currently not possible. If there was something specific with <code>session.Query(...).Exec()</code> it would be useful but there isn't as far as I know.</p>
  <p>I was thinking to <code>SELECT</code> by <code>email_address</code> before proceeding with <code>INSERT</code> if there was no matching record but as you can guess this is not feasible because by the time I <code>INSERT</code>ed a new record after <code>SELECT</code>, some other operation could have <code>INSERT</code>ed a new record with same email address.</p>
  <p>How do we handle such scenario?</p>