<p>Give this a whirl, Mark:</p>

<pre><code>{% for image in craft.assets.relatedTo(tags).order('elements.dateCreated desc') %}
</code></pre>

CC BY-SA 3.0
2014-10-27T16:29:09.863Z
Thanks, Brad! I've implemented this and will close it out when I see those errors aren't appearing.
Mark J. Reeves
CC BY-SA 3.0
2014-10-28T13:30:50.477Z
Looks like this has resolved the cache errors. Thanks again!
Mark J. Reeves
CC BY-SA 3.0
2014-10-24T17:12:12.513Z
2014-10-24T17:12:12.513Z
Brad Bell
<p>Following up on: <a href="https://craftcms.stackexchange.com/questions/4329/deleting-stale-template-caches-failed">Deleting Stale Template Caches Failed</a></p>

<p>Here's what I'm assuming is the tag throwing the error in the DeleteStaleTemplateCaches task. How would I disambiguate this? I'm not sure dateCreated desc is the default for relatedTo/assets, either.</p>

<pre><code>{% for image in craft.assets.relatedTo(tags).order('dateCreated desc') %}
</code></pre>

<p>Here's the log error:</p>

<pre><code>2014/10/23 17:10:34 [error] [system.db.CDbCommand] CDbCommand::fetchAll() failed: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'dateCreated' in order clause is ambiguous. The SQL statement executed was: SELECT `elements`.`id`
FROM `craft_elements` `elements`
JOIN `craft_elements_i18n` `elements_i18n` ON elements_i18n.elementId = elements.id
JOIN `craft_content` `content` ON content.elementId = elements.id
LEFT JOIN `craft_relations` `sources1` ON (sources1.targetId = elements.id)
LEFT JOIN `craft_relations` `targets1` ON (targets1.sourceId = elements.id)
JOIN `craft_assetfiles` `assetfiles` ON assetfiles.id = elements.id
WHERE (((((elements_i18n.locale = :locale) AND (content.locale = :locale)) AND (elements.archived = 0)) AND (elements.enabled = 1)) AND (elements_i18n.enabled = 1)) AND (((sources1.sourceId=:sources1sourceId1) OR (sources1.sourceId=:sources1sourceId2) OR (sources1.sourceId=:sources1sourceId3) OR (sources1.sourceId=:sources1sourceId4) OR (sources1.sourceId=:sources1sourceId5) OR (sources1.sourceId=:sources1sourceId6) OR (sources1.sourceId=:sources1sourceId7) OR (sources1.sourceId=:sources1sourceId8) OR (sources1.sourceId=:sources1sourceId9) OR (sources1.sourceId=:sources1sourceId10) OR (sources1.sourceId=:sources1sourceId11) OR (sources1.sourceId=:sources1sourceId12) OR (sources1.sourceId=:sources1sourceId13) OR (sources1.sourceId=:sources1sourceId14) OR (sources1.sourceId=:sources1sourceId15) OR (sources1.sourceId=:sources1sourceId16) OR (sources1.sourceId=:sources1sourceId17) OR (sources1.sourceId=:sources1sourceId18) OR (sources1.sourceId=:sources1sourceId19) OR (sources1.sourceId=:sources1sourceId20) OR (sources1.sourceId=:sources1sourceId21) OR (sources1.sourceId=:sources1sourceId22) OR (sources1.sourceId=:sources1sourceId23) OR (sources1.sourceId=:sources1sourceId24) OR (sources1.sourceId=:sources1sourceId25) OR (sources1.sourceId=:sources1sourceId26) OR (sources1.sourceId=:sources1sourceId27) OR (sources1.sourceId=:sources1sourceId28) OR (sources1.sourceId=:sources1sourceId29) OR (sources1.sourceId=:sources1sourceId30) OR (sources1.sourceId=:sources1sourceId31) OR (sources1.sourceId=:sources1sourceId32) OR (sources1.sourceId=:sources1sourceId33) OR (sources1.sourceId=:sources1sourceId34) OR (sources1.sourceId=:sources1sourceId35) OR (sources1.sourceId=:sources1sourceId36) OR (sources1.sourceId=:sources1sourceId37) OR (sources1.sourceId=:sources1sourceId38) OR (sources1.sourceId=:sources1sourceId39) OR (sources1.sourceId=:sources1sourceId40) OR (sources1.sourceId=:sources1sourceId41) OR (sources1.sourceId=:sources1sourceId42) OR (sources1.sourceId=:sources1sourceId43) OR (sources1.sourceId=:sources1sourceId44) OR (sources1.sourceId=:sources1sourceId45) OR (sources1.sourceId=:sources1sourceId46) OR (sources1.sourceId=:sources1sourceId47) OR (sources1.sourceId=:sources1sourceId48) OR (sources1.sourceId=:sources1sourceId49) OR (sources1.sourceId=:sources1sourceId50) OR (sources1.sourceId=:sources1sourceId51) OR (sources1.sourceId=:sources1sourceId52) OR (sources1.sourceId=:sources1sourceId53) OR (sources1.sourceId=:sources1sourceId54) OR (sources1.sourceId=:sources1sourceId55) OR (sources1.sourceId=:sources1sourceId56) OR (sources1.sourceId=:sources1sourceId57) OR (sources1.sourceId=:sources1sourceId58) OR (sources1.sourceId=:sources1sourceId59) OR (sources1.sourceId=:sources1sourceId60) OR (sources1.sourceId=:sources1sourceId61) OR (sources1.sourceId=:sources1sourceId62) OR (sources1.sourceId=:sources1sourceId63) OR (sources1.sourceId=:sources1sourceId64)) OR ((targets1.targetId=:targets1targetId1) OR (targets1.targetId=:targets1targetId2) OR (targets1.targetId=:targets1targetId3) OR (targets1.targetId=:targets1targetId4) OR (targets1.targetId=:targets1targetId5) OR (targets1.targetId=:targets1targetId6) OR (targets1.targetId=:targets1targetId7) OR (targets1.targetId=:targets1targetId8) OR (targets1.targetId=:targets1targetId9) OR (targets1.targetId=:targets1targetId10) OR (targets1.targetId=:targets1targetId11) OR (targets1.targetId=:targets1targetId12) OR (targets1.targetId=:targets1targetId13) OR (targets1.targetId=:targets1targetId14) OR (targets1.targetId=:targets1targetId15) OR (targets1.targetId=:targets1targetId16) OR (targets1.targetId=:targets1targetId17) OR (targets1.targetId=:targets1targetId18) OR (targets1.targetId=:targets1targetId19) OR (targets1.targetId=:targets1targetId20) OR (targets1.targetId=:targets1targetId21) OR (targets1.targetId=:targets1targetId22) OR (targets1.targetId=:targets1targetId23) OR (targets1.targetId=:targets1targetId24) OR (targets1.targetId=:targets1targetId25) OR (targets1.targetId=:targets1targetId26) OR (targets1.targetId=:targets1targetId27) OR (targets1.targetId=:targets1targetId28) OR (targets1.targetId=:targets1targetId29) OR (targets1.targetId=:targets1targetId30) OR (targets1.targetId=:targets1targetId31) OR (targets1.targetId=:targets1targetId32) OR (targets1.targetId=:targets1targetId33) OR (targets1.targetId=:targets1targetId34) OR (targets1.targetId=:targets1targetId35) OR (targets1.targetId=:targets1targetId36) OR (targets1.targetId=:targets1targetId37) OR (targets1.targetId=:targets1targetId38) OR (targets1.targetId=:targets1targetId39) OR (targets1.targetId=:targets1targetId40) OR (targets1.targetId=:targets1targetId41) OR (targets1.targetId=:targets1targetId42) OR (targets1.targetId=:targets1targetId43) OR (targets1.targetId=:targets1targetId44) OR (targets1.targetId=:targets1targetId45) OR (targets1.targetId=:targets1targetId46) OR (targets1.targetId=:targets1targetId47) OR (targets1.targetId=:targets1targetId48) OR (targets1.targetId=:targets1targetId49) OR (targets1.targetId=:targets1targetId50) OR (targets1.targetId=:targets1targetId51) OR (targets1.targetId=:targets1targetId52) OR (targets1.targetId=:targets1targetId53) OR (targets1.targetId=:targets1targetId54) OR (targets1.targetId=:targets1targetId55) OR (targets1.targetId=:targets1targetId56) OR (targets1.targetId=:targets1targetId57) OR (targets1.targetId=:targets1targetId58) OR (targets1.targetId=:targets1targetId59) OR (targets1.targetId=:targets1targetId60) OR (targets1.targetId=:targets1targetId61) OR (targets1.targetId=:targets1targetId62) OR (targets1.targetId=:targets1targetId63) OR (targets1.targetId=:targets1targetId64)))
GROUP BY `elements`.`id`
ORDER BY `dateCreated` DESC LIMIT 100.
2014/10/23 17:10:34 [error] [application] Encountered an error running task 778 (DeleteStaleTemplateCaches), step 26 of 351: An exception was thrown: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'dateCreated' in order clause is ambiguous 
</code></pre>

<p>Thanks!</p>

<p>Mark</p>

<assets><relations><cache><ordering>
How to fix "ambiguous" column error generated when DeleteStaleTemplateCaches is run?
# How to fix "ambiguous" column error generated when DeleteStaleTemplateCaches is run?

(Asked by: username_0 on 2014-10-24)

Following up on: Deleting Stale Template Caches Failed
Here's what I'm assuming is the tag throwing the error in the DeleteStaleTemplateCaches task. How would I disambiguate this? I'm not sure dateCreated desc is the default for relatedTo/assets, either.
{% for image in craft.assets.relatedTo(tags).order('dateCreated desc') %}

Here's the log error:
2014/10/23 17:10:34 [error] [system.db.CDbCommand] CDbCommand::fetchAll() failed: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'dateCreated' in order clause is ambiguous. The SQL statement executed was: SELECT `elements`.`id`
FROM `craft_elements` `elements`
JOIN `craft_elements_i18n` `elements_i18n` ON elements_i18n.elementId = elements.id
JOIN `craft_content` `content` ON content.elementId = elements.id
LEFT JOIN `craft_relations` `sources1` ON (sources1.targetId = elements.id)
LEFT JOIN `craft_relations` `targets1` ON (targets1.sourceId = elements.id)
JOIN `craft_assetfiles` `assetfiles` ON assetfiles.id = elements.id
WHERE (((((elements_i18n.locale = :locale) AND (content.locale = :locale)) AND (elements.archived = 0)) AND (elements.enabled = 1)) AND (elements_i18n.enabled = 1)) AND (((sources1.sourceId=:sources1sourceId1) OR (sources1.sourceId=:sources1sourceId2) OR (sources1.sourceId=:sources1sourceId3) OR (sources1.sourceId=:sources1sourceId4) OR (sources1.sourceId=:sources1sourceId5) OR (sources1.sourceId=:sources1sourceId6) OR (sources1.sourceId=:sources1sourceId7) OR (sources1.sourceId=:sources1sourceId8) OR (sources1.sourceId=:sources1sourceId9) OR (sources1.sourceId=:sources1sourceId10) OR (sources1.sourceId=:sources1sourceId11) OR (sources1.sourceId=:sources1sourceId12) OR (sources1.sourceId=:sources1sourceId13) OR (sources1.sourceId=:sources1sourceId14) OR (sources1.sourceId=:sources1sourceId15) OR (sources1.sourceId=:sources1sourceId16) OR (sources1.sourceId=:sources1sourceId17) OR (sources1.sourceId=:sources1sourceId18) OR (sources1.sourceId=:sources1sourceId19) OR (sources1.sourceId=:sources1sourceId20) OR (sources1.sourceId=:sources1sourceId21) OR (sources1.sourceId=:sources1sourceId22) OR (sources1.sourceId=:sources1sourceId23) OR (sources1.sourceId=:sources1sourceId24) OR (sources1.sourceId=:sources1sourceId25) OR (sources1.sourceId=:sources1sourceId26) OR (sources1.sourceId=:sources1sourceId27) OR (sources1.sourceId=:sources1sourceId28) OR (sources1.sourceId=:sources1sourceId29) OR (sources1.sourceId=:sources1sourceId30) OR (sources1.sourceId=:sources1sourceId31) OR (sources1.sourceId=:sources1sourceId32) OR (sources1.sourceId=:sources1sourceId33) OR (sources1.sourceId=:sources1sourceId34) OR (sources1.sourceId=:sources1sourceId35) OR (sources1.sourceId=:sources1sourceId36) OR (sources1.sourceId=:sources1sourceId37) OR (sources1.sourceId=:sources1sourceId38) OR (sources1.sourceId=:sources1sourceId39) OR (sources1.sourceId=:sources1sourceId40) OR (sources1.sourceId=:sources1sourceId41) OR (sources1.sourceId=:sources1sourceId42) OR (sources1.sourceId=:sources1sourceId43) OR (sources1.sourceId=:sources1sourceId44) OR (sources1.sourceId=:sources1sourceId45) OR (sources1.sourceId=:sources1sourceId46) OR (sources1.sourceId=:sources1sourceId47) OR (sources1.sourceId=:sources1sourceId48) OR (sources1.sourceId=:sources1sourceId49) OR (sources1.sourceId=:sources1sourceId50) OR (sources1.sourceId=:sources1sourceId51) OR (sources1.sourceId=:sources1sourceId52) OR (sources1.sourceId=:sources1sourceId53) OR (sources1.sourceId=:sources1sourceId54) OR (sources1.sourceId=:sources1sourceId55) OR (sources1.sourceId=:sources1sourceId56) OR (sources1.sourceId=:sources1sourceId57) OR (sources1.sourceId=:sources1sourceId58) OR (sources1.sourceId=:sources1sourceId59) OR (sources1.sourceId=:sources1sourceId60) OR (sources1.sourceId=:sources1sourceId61) OR (sources1.sourceId=:sources1sourceId62) OR (sources1.sourceId=:sources1sourceId63) OR (sources1.sourceId=:sources1sourceId64)) OR ((targets1.targetId=:targets1targetId1) OR (targets1.targetId=:targets1targetId2) OR (targets1.targetId=:targets1targetId3) OR (targets1.targetId=:targets1targetId4) OR (targets1.targetId=:targets1targetId5) OR (targets1.targetId=:targets1targetId6) OR (targets1.targetId=:targets1targetId7) OR (targets1.targetId=:targets1targetId8) OR (targets1.targetId=:targets1targetId9) OR (targets1.targetId=:targets1targetId10) OR (targets1.targetId=:targets1targetId11) OR (targets1.targetId=:targets1targetId12) OR (targets1.targetId=:targets1targetId13) OR (targets1.targetId=:targets1targetId14) OR (targets1.targetId=:targets1targetId15) OR (targets1.targetId=:targets1targetId16) OR (targets1.targetId=:targets1targetId17) OR (targets1.targetId=:targets1targetId18) OR (targets1.targetId=:targets1targetId19) OR (targets1.targetId=:targets1targetId20) OR (targets1.targetId=:targets1targetId21) OR (targets1.targetId=:targets1targetId22) OR (targets1.targetId=:targets1targetId23) OR (targets1.targetId=:targets1targetId24) OR (targets1.targetId=:targets1targetId25) OR (targets1.targetId=:targets1targetId26) OR (targets1.targetId=:targets1targetId27) OR (targets1.targetId=:targets1targetId28) OR (targets1.targetId=:targets1targetId29) OR (targets1.targetId=:targets1targetId30) OR (targets1.targetId=:targets1targetId31) OR (targets1.targetId=:targets1targetId32) OR (targets1.targetId=:targets1targetId33) OR (targets1.targetId=:targets1targetId34) OR (targets1.targetId=:targets1targetId35) OR (targets1.targetId=:targets1targetId36) OR (targets1.targetId=:targets1targetId37) OR (targets1.targetId=:targets1targetId38) OR (targets1.targetId=:targets1targetId39) OR (targets1.targetId=:targets1targetId40) OR (targets1.targetId=:targets1targetId41) OR (targets1.targetId=:targets1targetId42) OR (targets1.targetId=:targets1targetId43) OR (targets1.targetId=:targets1targetId44) OR (targets1.targetId=:targets1targetId45) OR (targets1.targetId=:targets1targetId46) OR (targets1.targetId=:targets1targetId47) OR (targets1.targetId=:targets1targetId48) OR (targets1.targetId=:targets1targetId49) OR (targets1.targetId=:targets1targetId50) OR (targets1.targetId=:targets1targetId51) OR (targets1.targetId=:targets1targetId52) OR (targets1.targetId=:targets1targetId53) OR (targets1.targetId=:targets1targetId54) OR (targets1.targetId=:targets1targetId55) OR (targets1.targetId=:targets1targetId56) OR (targets1.targetId=:targets1targetId57) OR (targets1.targetId=:targets1targetId58) OR (targets1.targetId=:targets1targetId59) OR (targets1.targetId=:targets1targetId60) OR (targets1.targetId=:targets1targetId61) OR (targets1.targetId=:targets1targetId62) OR (targets1.targetId=:targets1targetId63) OR (targets1.targetId=:targets1targetId64)))
GROUP BY `elements`.`id`
ORDER BY `dateCreated` DESC LIMIT 100.
2014/10/23 17:10:34 [error] [application] Encountered an error running task 778 (DeleteStaleTemplateCaches), step 26 of 351: An exception was thrown: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'dateCreated' in order clause is ambiguous 

Thanks!
Mark

**Tags:** assets, relations, cache, ordering


---

## 1 answers

### Answer by username_1 on 2014-10-24. Score: 6 | Accepted answer

Give this a whirl, Mark:
{% for image in craft.assets.relatedTo(tags).order('elements.dateCreated desc') %}

> **Comments:**
> **username_0**: Thanks, Brad! I've implemented this and will close it out when I see those errors aren't appearing.  
> **username_0**: Looks like this has resolved the cache errors. Thanks again!  

---

