<p>Similar to <a href="https://craftcms.stackexchange.com/questions/4343/how-to-fix-ambiguous-column-error-generated-when-deletestaletemplatecaches-is">How to fix &quot;ambiguous&quot; column error generated when DeleteStaleTemplateCaches is run?</a></p>

<p>But somewhere, in one of your templates that are using the <code>{% cache %}</code> tag, you're ordering by <code>dateCreated</code> in your template and it should be <code>elements.dateCreated</code>.</p>

<p>The <code>{% cache %}</code> tag has already picked up the invalid Twig code and cached it to the database so whenever the DeleteStaleTemplateCaches task tries to run, it trips up on the invalid Twig.</p>

<p>So after you fix the original error, you'll want to clear your template caches using the Clear Caches tool under settings in the control panel to purge the invalid cached code as well.</p>

CC BY-SA 3.0
2016-10-27T17:32:23.413Z
Thanks Brad. Out of curiosity, why does this only apply to `dateCreated `?
richardpixel
CC BY-SA 3.0
2016-10-27T18:05:16.500Z
Just a side-effect of the way we build the main elements query on the back-end.  Not an issue anymore on the upcoming Craft 3.
Brad Bell
CC BY-SA 3.0
2016-10-27T18:11:45.967Z
Okay, good to know :)
richardpixel
CC BY-SA 3.0
2016-10-27T16:44:00.943Z
2016-10-27T16:44:00.943Z
Brad Bell
<p>I've got a few issues with a site but I'll try to separate in case they're not related.</p>

<p>Firstly I keep getting "Failed task - Deleting stale template caches".</p>

<p>I've looked in the logs and some messages include:</p>

<blockquote>
  <p>Encountered an error running task 3 (DeleteStaleTemplateCaches), step 156 of 166: An exception was thrown: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'dateCreated' in order clause is ambiguous</p>
</blockquote>

<p>and</p>

<blockquote>
  <p>CDbCommand::fetchAll() failed: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'dateCreated' in order clause is ambiguous. The SQL statement executed was: SELECT <code>elements</code>.<code>id</code>, <code>entries</code>.<code>sectionId</code>, <code>entries</code>.<code>typeId</code>, <code>entries</code>.<code>authorId</code>, <code>entries</code>.<code>postDate</code>, <code>entries</code>.<code>expiryDate</code></p>
</blockquote>

<error-message><tasks>
Failed Task and various errors in logs
# Failed Task and various errors in logs

(Asked by: username_0 on 2016-10-27)

I've got a few issues with a site but I'll try to separate in case they're not related.

Firstly I keep getting "Failed task - Deleting stale template caches".

I've looked in the logs and some messages include:

> Encountered an error running task 3 (DeleteStaleTemplateCaches), step 156 of 166: An exception was thrown: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'dateCreated' in order clause is ambiguous

and

> CDbCommand::fetchAll() failed: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'dateCreated' in order clause is ambiguous. The SQL statement executed was: SELECT `elements`.`id`, `entries`.`sectionId`, `entries`.`typeId`, `entries`.`authorId`, `entries`.`postDate`, `entries`.`expiryDate`

**Tags:** error-message, tasks

---

## 1 answers

### Answer by username_1 on 2016-10-27. Score: 2 | Accepted answer

Similar to [How to fix "ambiguous" column error generated when DeleteStaleTemplateCaches is run?](https://craftcms.stackexchange.com/questions/4343/how-to-fix-ambiguous-column-error-generated-when-deletestaletemplatecaches-is)

But somewhere, in one of your templates that are using the `{% cache %}` tag, you're ordering by `dateCreated` in your template and it should be `elements.dateCreated`.

The `{% cache %}` tag has already picked up the invalid Twig code and cached it to the database so whenever the DeleteStaleTemplateCaches task tries to run, it trips up on the invalid Twig.

So after you fix the original error, you'll want to clear your template caches using the Clear Caches tool under settings in the control panel to purge the invalid cached code as well.

> **Comments:**
> **username_0**: Thanks Brad. Out of curiosity, why does this only apply to `dateCreated `?  
> **username_1**: Just a side-effect of the way we build the main elements query on the back-end.  Not an issue anymore on the upcoming Craft 3.  
> **username_0**: Okay, good to know :)  

---

