title: how to replace SQLAlchemy mapper deprecated "order_by" option?
body: <p>I have a <code>Single Table Inheritance</code> sqlalchemy orm model that looks like this:</p>
  <pre class="lang-py prettyprint-override"><code>class Human(Base):
      
      __tablename__ = &quot;human&quot;
  
      id = Column(Integer, primary_key=True)
      name = Column(String)
      index = Column(Integer)
      parent_id = Column(Integer, ForeignKey(&quot;human.id&quot;), nullable=True)
      type = Column(String)
      parent = relationship(
          &quot;Human&quot;,
          cascade=&quot;save-update, merge&quot;,
          backref=backref(&quot;children&quot;, cascade=&quot;all&quot;),
          lazy=False,
          remote_side=&quot;Human.id&quot;,
      )
  
      __mapper_args__ = {&quot;polymorphic_on&quot;: type, &quot;polymorphic_identity&quot;: &quot;human&quot;}
  
  class Parent(Human):
  
      __mapper_args__ = {&quot;polymorphic_identity&quot;: &quot;parent&quot;}
  
  class Child(Human):
  
      hobby = Column(String)
      pet = Column(String)
  
      __mapper_args__ = {&quot;polymorphic_identity&quot;: &quot;child&quot;}
  </code></pre>
  <p>This relationship looks like a nested tree, where I can query the oldest person (i.e: great grandparent, using <code>query.get(1)</code>) and then in his <code>children</code> attribute all his <code>children</code> will be present, and each child will also have their <code>children</code> present.
  This relationship works just fine in every aspect.</p>
  <p>I want the <code>children</code> that are queried when I query a <code>Parent</code> (due to lazy=False) to be ordered by the <code>index</code> column not the <code>id</code> column (which is the default).</p>
  <p>I was achieving this by adding the following <code>&quot;order_by&quot;: &quot;index&quot;</code> to the <code>Human</code> class' <code>__mapper_args__</code> as follows:</p>
  <pre class="lang-py prettyprint-override"><code>__mapper_args__ = {&quot;polymorphic_on&quot;: type, &quot;polymorphic_identity&quot;: &quot;human&quot;, &quot;order_by&quot;: &quot;index&quot;}
  </code></pre>
  <p>I recently found out that in SQLAlchemy the <code>order_by</code> argument in the <code>mapper()</code> class is deprecated since SQLAlchemy version <code>1.1</code> (<a href="https://docs.sqlalchemy.org/en/13/orm/mapping_api.html?highlight=mapper#sqlalchemy.orm.mapper.params.order_by" rel="nofollow noreferrer">reference</a>). And that they say to use <code>Query.order_by()</code> to order the set instead, which is not working for me for some reason.</p>
  <p>I have tried the following:</p>
  <ul>
  <li>adding <code>order_by</code> to the query, <code>session.query(Human).order_by(Human.index).get(1)</code></li>
  <li>adding <code>order_by=&quot;Human.index&quot;</code> to the <code>relationship()</code> in the <code>Human</code> class.</li>
  </ul>
  <p>these two methods are not working.</p>
  <p>How can I achieve the same effect as the <code>order_by</code> in <code>mapper()</code> now that it is being deprecated?</p>