MySQL On Update not triggering for Django/TastyPie REST API
<p>We have a resource table which has a field <code>last_updated</code> which we setup with mysql-workbench to have the following properties:</p>

<p>Datatype: <code>TIMESTAMP</code></p>

<p>NN (NotNull) is <code>checked</code></p>

<p>Default: <code>CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP</code></p>

<p>When I modify a row through the workbench and apply it, the <code>last_updated</code> field properly updates.</p>

<p>When I use the REST api we've setup, and issue a put:</p>

<pre><code>update = requests.put('http://127.0.0.1:8000/api/resources/16',
            data=json.dumps(dict(status="/api/status/4", timeout=timeout_time)),
            headers=HEADER)
</code></pre>

<p>I can properly change any of the values (including status and timeout, and receive a 204 response), but <code>last_updated</code> does not update.</p>

<p><a href="https://docs.djangoproject.com/en/1.9/ref/models/instances/#how-django-knows-to-update-vs-insert" rel="nofollow">Django's model documentation</a> says in this case it should be sending an <code>UPDATE</code>.</p>

<p>Anyone have and ideas on why it's missing these updates? </p>

<p>I can provide further details regarding our specific Django/tastypie setup, but as long as they are issuing an <code>UPDATE</code>, they should be triggering the databases <code>ON UPDATE</code>.</p>

<p>I suspect that the UPDATE statement issued by Django may be including an assignment to the <code>last_updated</code> column. This is just a guess, there's not enough information provided.</p>

<p>But if the Django model contains the <code>last_updated</code> column, and that column is fetched from the database into the model, I believe a <strong>save()</strong> will assign a value to the <code>last_updated</code> column, in the UPDATE statement. </p>

<p><a href="https://docs.djangoproject.com/en/1.9/ref/models/instances/#specifying-which-fields-to-save" rel="nofollow">https://docs.djangoproject.com/en/1.9/ref/models/instances/#specifying-which-fields-to-save</a></p>

<hr>

<p>Consider the behavior when we issue an UPDATE statement like this:</p>

<pre><code> UPDATE mytable
    SET last_updated = last_updated
      , some_col = 'some_value'
  WHERE id = 42 
</code></pre>

<p>Because the UPDATE statement is assigning a value to the <code>last_updated</code> column, the automatic assignment to the timestamp column won't happen. The value assigned in the statement takes precedence.</p>

<p>To get the automatic assignment to <code>last_updated</code>, that column has to be omitted from the <code>SET</code> clause, e.g.</p>

<pre><code> UPDATE mytable
    SET some_col = 'some_value'
  WHERE id = 42 
</code></pre>

<p>To debug this, you'd want to inspect the actual SQL statement.</p>

python mysql django mysql-workbench tastypie