title: Cassandra TTL gets set to 0 on primary key if no TTL is specified on an update, but if it is, the TTL on the primary key does not change
body: <p>This behavior in Cassandra seems counter-intuitive and I want to know why this is happening, and possibly work around this.</p>
  
  <hr>
  
  <p>Imagine I have a table with three columns: <strong><code>pk</code></strong>, the primary key, a <code>text</code> type, <strong><code>foo</code></strong>, a <code>bigint</code>, and <strong><code>bar</code></strong>, another <code>text</code>.</p>
  
  <pre><code>insert into keyspace.table (pk, foo, bar) values ('first', 1, 'test') using ttl 60;
  </code></pre>
  
  <p>This creates a row in my table that has a time-to-live of 60 seconds. Looking at it, it looks like this:</p>
  
  <pre><code>  pk  | foo | bar
  ------------------
  first |  1  | test
  </code></pre>
  
  <p>Now I do:</p>
  
  <pre><code>update keyspace.table using ttl 10 set bar='change' where pk='first';
  </code></pre>
  
  <p>And then, watching the row, I see it undergo the following changes:</p>
  
  <pre><code>  pk  | foo | bar
  --------------------
  first |  1  | change
  first |  1  | &lt;&lt;null&gt;&gt;  // after 10 seconds
     &lt;&lt; deleted &gt;&gt;        // after the initial 60 seconds
  </code></pre>
  
  <p>All well and good. What I wanted was for <code>bar</code>'s time-to-live to change, but nothing else, especially not the primary key. This behavior was expected.</p>
  
  <hr>
  
  <p><em>However</em>, if my update doesn't have a <code>ttl</code> in it, or it's set to 0:</p>
  
  <pre><code>update keyspace.table set bar='change' where pk='first';
  </code></pre>
  
  <p>Then I see this behavior over time instead.</p>
  
  <pre><code>  pk  | foo | bar
  --------------------
  first |  1  | change
  first |  0  | change   // after the initial 60 seconds
  </code></pre>
  
  <p>In other words, the row is never deleted. <code>foo</code> hadn't been changed, so its time-to-live was still in effect and after it passed the value was deleted (set to 0). But <code>pk</code> did have its time-to-live changed. This is totally unexpected.</p>
  
  <p>Why does the primary key's time-to-live change only if I don't specify the time-to-live in the update? And how can I work around this so that the primary key's time-to-live will only change if I explicitly say to do so?</p>
  
  <p><em>Edit</em> I also found that if I use a time-to-live that's higher than the initial one it also seems to change the time-to-live on the primary key.</p>
  
  <pre><code>update keyspace.table using ttl 70 set bar='change' where pk='first';
  
    pk  | foo | bar
  --------------------
  first |  1  | change
  first |  0  | change   // after the initial 60 seconds
     &lt;&lt; deleted &gt;&gt;       // after the 70 seconds
  </code></pre>