Related Entries not sorting correctly Craft 3.0.16.1

I have a relationship that is outputting entries, but the entries are not in the order as selected.

{# not sorting correctly  - **fieldName** is a variable passed via include #}
{% set relatedEntries = publications.relatedTo(
  {sourceElement: country, field: fieldName}
) %}


I copied this code from the documentation and change field names to work

{# copied from docs but template errors #}
{% set relatedEntries = craft.entries({
    relatedTo: { sourceElement: country, field: fieldName },
    orderBy:     "sortOrder",
    limit:     null
}) %}


but this throws a database exception error


  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sortOrder' in
  'order clause'


How do I get related entries to sort on the order as selected in the entries field?

********************UPDATE**********************

HERE IS THE FULL TEMPLATE

  &lt;li class="accordion-item {#{% if loop.first %}is-active{% endif %}#}" data-accordion-item&gt;
    &lt;!-- Accordion tab title --&gt;
    &lt;a href="#" class="accordion-title"&gt;
      {{ categoryTitle  }}
      &lt;i class="icon-chevron-right"&gt;&lt;/i&gt;&lt;!-- /.arrow right --&gt;
    &lt;/a&gt;

    &lt;!-- Accordion tab content: it would start in the open state due to using the `is-active` state class. --&gt;
    &lt;div class="accordion-content" data-tab-content&gt;

      {# start tabbed content #}
      &lt;ul class="tabs" data-tabs id="cat-{{ hash }} "&gt;

       {# inside categories list countries #}
      {% for country in countries.all() %}  
        &lt;li class="tabs-title {#{% if loop.first %}is-active{% endif %}#}"&gt;&lt;a href="#{{ country.slug }}" aria-selected="true" data-tabs-target="{{ country.slug }}"&gt;{{ country.title }}&lt;/a&gt;&lt;/li&gt;
      {% endfor %} {# end countries loop #}
      &lt;/ul&gt;


      &lt;div class="tabs-content" data-tabs-content="cat-{{ hash }}"&gt;
        {% for country in countries.all() %}
        &lt;div class="tabs-panel {#{% if loop.first %}is-active{% endif %}#}" id="{{ country.slug }}"&gt;
          &lt;ul class="no-bullet pubs full-width"&gt;

            {# finally output entries that are in the category and country #}

            {# not sorting correctly #}
            {% set relatedEntries = publications.relatedTo(                   {sourceElement: country, field: fieldName}
            ) %}

            {# error about method ('all') on an array #}
            {# {% set relatedEntries = country[fieldName].all() %} #}

            {# 
            // copied from docs but template errors
            {% set relatedEntries = craft.entries({
                relatedTo: { sourceElement: country, field: fieldName },
                orderBy:     "sortOrder",
                limit:     null
            }) %} #}



            {% for entry in relatedEntries.all() %}

              {% for block in entry.Documents.all() %}
                {% if block.externalLink|length %}
                  {# use external link field #}
                  {% set link = block.externalLink %}
                {% else %}
                  {# use uploaded file #}
                  {% set link = block.pdfOrFile.one().url ?? null%}
                {% endif %}

                &lt;li&gt;&lt;a href="{{ link }}" target="_blank"&gt;
                  &lt;p&gt;{{ entry.title }}&lt;/p&gt;
                  &lt;div class="icon-holder"&gt;
                    &lt;img src="/assets/img/next.svg" alt="" class="go-icon"&gt;
                  &lt;/div&gt;&lt;!-- /.icon-holder --&gt;&lt;/a&gt;
                &lt;/li&gt;
              {% endfor %}{#end link #}

              {% else %}
              {# output nothing for no results #}
            {% endfor %}

          &lt;/ul&gt;&lt;!-- /.no-bullet pubs --&gt;
        &lt;/div&gt;
        {% endfor %}
      &lt;/div&gt;
      {# end tabbed content #}      

    &lt;/div&gt;&lt;!--accordion content--&gt;
  &lt;/li&gt;
When you include templates and pass multiple different variables to them with different types that may have a different field layout it is required to code defensively and check for variable types. Actually you should never print something without further checking. Anything bad that could happen will happen. Especially in such a case

{% if country[fieldhandle] is defined and country[fieldhandle] is not empty and  %}
    {% for element in country.getFieldValue(fieldhandle).all() %}
        {{ element.title }}
    {% endfor %}
{% endif %}


You may as well include a instance of check

{% if element.getFieldValue(fieldHandle) is instance of('craft\\elements\\db\\ElementQuery') %}
    {% set relatedElements = element.getFieldValue(fieldHandle).all() %}
{% else %}
    // eager loading, the result is an array, no need to fetch it
    {% set relatedElements = element.getFieldValue(fieldHandle) %}
{% endif %}