<p>So this was me being a bit of a numpty here. I was calling the field the wrong way. Doing the following obeys the sort order from the field:</p>

<pre><code>{% for entry in block.hostingPackage %}
</code></pre>

CC BY-SA 4.0
2018-08-29T15:50:18.350Z
2018-08-29T15:50:18.350Z
richardpixel
<p>I'm getting the following SQL error when trying to simply order related entries in a Matrix block. Anyway around this?</p>

<blockquote>
  <p>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'sortOrder' in order clause is ambiguous. The SQL statement executed was: SELECT <code>elements</code>.<code>id</code>, <code>elements</code>.<code>type</code>, <code>elements</code>.<code>enabled</code>, <code>elements</code>.<code>archived</code>, <code>elements</code>.<code>dateCreated</code>, <code>elements</code>.<code>dateUpdated</code>, ...</p>
</blockquote>

<p>This is what's throwing the error:</p>

<pre><code>{% set package = craft.entries({
relatedTo: block,
order: 'sortOrder'}) %}
</code></pre>

CC BY-SA 4.0
2018-08-28T16:25:31.023Z
Are you working in Craft 2 or Craft 3?
a-am
CC BY-SA 4.0
2018-08-28T16:40:18.413Z
I'm using Craft 2. Updated the question.
richardpixel
CC BY-SA 4.0
2018-08-28T17:39:29.773Z
I don't believe you need the `order: 'sortOrder'` it does that by default when you access related entries.  Here is another post that may be of help. https://craftcms.stackexchange.com/questions/4654/custom-order-related-entries/4657#4657
a-am
CC BY-SA 4.0
2018-08-29T08:51:37.443Z
Thanks @aran but no, it's not obeying the sortOrder. I only seem to come across this issue on a related entry within a Matrix field block.
richardpixel
<matrix><relations><ordering><relatedto><sorting>
SQL error when trying to use sortOrder on Matrix related block (Craft 2)
# SQL error when trying to use sortOrder on Matrix related block (Craft 2)

(Asked by: username_0 on 2018-08-28)

I'm getting the following SQL error when trying to simply order related entries in a Matrix block. Anyway around this?

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'sortOrder' in order clause is ambiguous. The SQL statement executed was: SELECT elements.id, elements.type, elements.enabled, elements.archived, elements.dateCreated, elements.dateUpdated, ...

This is what's throwing the error:
{% set package = craft.entries({
relatedTo: block,
order: 'sortOrder'}) %}

**Tags:** matrix, relations, ordering, relatedto, sorting
> **Comments:**
> **username_1**: Are you working in Craft 2 or Craft 3?  
> **username_0**: I'm using Craft 2. Updated the question.  
> **username_1**: I don't believe you need the `order: 'sortOrder'` it does that by default when you access related entries.  Here is another post that may be of help. https://craftcms.stackexchange.com/questions/4654/custom-order-related-entries/4657#4657  
> **username_0**: Thanks @aran but no, it's not obeying the sortOrder. I only seem to come across this issue on a related entry within a Matrix field block.  

---

## 1 answers

### Answer by username_0 on 2018-08-29. Score: 1

So this was me being a bit of a numpty here. I was calling the field the wrong way. Doing the following obeys the sort order from the field:
{% for entry in block.hostingPackage %}


---

