<p dir="auto"><strong>Migrated issue, originally created by Ronny Pfannschmidt (<a href="https://github.com/RonnyPfannschmidt">@RonnyPfannschmidt</a>)</strong></p>
<p dir="auto">this problem has shown up when porting to oracle, which reported a random error uppon seeing the sql</p>
<p dir="auto">after inspection i was sure the code was certainly wrong - at least the join condition makes absolutely no sense there</p>
<p dir="auto">imho it should be an error to pass a relationship there</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, Session

Base = declarative_base()

class Fun(Base):
    __tablename__ = 'fun'
    id = Column(Integer, primary_key=True)
    pos = Column(Integer)

class FunDetail(Base):
    __tablename__ = 'fun_detail'
    id = Column(Integer, primary_key=True)
    fun_id = Column(Integer, ForeignKey('fun.id'))
    fun = relationship(Fun)
    pos = Column(Integer)

s = Session()
print s.query(FunDetail).order_by(FunDetail.pos, FunDetail.fun)
"><pre class="notranslate"><code class="notranslate">from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, Session

Base = declarative_base()

class Fun(Base):
    __tablename__ = 'fun'
    id = Column(Integer, primary_key=True)
    pos = Column(Integer)

class FunDetail(Base):
    __tablename__ = 'fun_detail'
    id = Column(Integer, primary_key=True)
    fun_id = Column(Integer, ForeignKey('fun.id'))
    fun = relationship(Fun)
    pos = Column(Integer)

s = Session()
print s.query(FunDetail).order_by(FunDetail.pos, FunDetail.fun)

</code></pre></div>
<p dir="auto">results in</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="SELECT fun_detail.id AS fun_detail_id, fun_detail.fun_id AS fun_detail_fun_id, fun_detail.pos AS fun_detail_pos 
FROM fun_detail ORDER BY fun_detail.pos, fun.id = fun_detail.fun_id"><pre class="notranslate"><code class="notranslate">SELECT fun_detail.id AS fun_detail_id, fun_detail.fun_id AS fun_detail_fun_id, fun_detail.pos AS fun_detail_pos 
FROM fun_detail ORDER BY fun_detail.pos, fun.id = fun_detail.fun_id
</code></pre></div>
<p dir="auto"><strong>Migrated issue, originally created by Michael Bayer (<a href="https://github.com/zzzeek">@zzzeek</a>)</strong></p>
<p dir="auto">consolidating <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="384616635" data-permission-text="Title is private" data-url="https://github.com/sqlalchemy/sqlalchemy/issues/1328" data-hovercard-type="issue" data-hovercard-url="/sqlalchemy/sqlalchemy/issues/1328/hovercard" href="https://github.com/sqlalchemy/sqlalchemy/issues/1328">#1328</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="384628315" data-permission-text="Title is private" data-url="https://github.com/sqlalchemy/sqlalchemy/issues/2797" data-hovercard-type="issue" data-hovercard-url="/sqlalchemy/sqlalchemy/issues/2797/hovercard" href="https://github.com/sqlalchemy/sqlalchemy/issues/2797">#2797</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="384628720" data-permission-text="Title is private" data-url="https://github.com/sqlalchemy/sqlalchemy/issues/2845" data-hovercard-type="issue" data-hovercard-url="/sqlalchemy/sqlalchemy/issues/2845/hovercard" href="https://github.com/sqlalchemy/sqlalchemy/issues/2845">#2845</a> as these are all dealing with the same thing from different angles.</p>
<p dir="auto">SomeClass.some_m2o, which refers to OtherEntity as a many-to-one, would be interpreted:</p>
<ol dir="auto">
<li>
<p dir="auto">for <code class="notranslate">sess.query(SomeClass.some_m2o)</code> as <code class="notranslate">sess.query(OtherEntity)</code></p>
</li>
<li>
<p dir="auto">for <code class="notranslate">sess.query(SomeClass).filter(SomeClass.some_m2o == OtherEntity)</code> as <code class="notranslate">filter(SomeClass.other_entity_id == OtherEntity.id)</code></p>
</li>
<li>
<p dir="auto">for <code class="notranslate">sess.query(SomeClass).order_by(SomeClass.some_m2o), group_by(SomeClass.some_m2o)</code>, as <code class="notranslate">order_by/group_by SomeClass.other_entity_id</code></p>
</li>
<li>
<p dir="auto">as is currently the case, <code class="notranslate">sess.query(SomeClass).filter(SomeClass.some_m2o == other_entity)</code> compares to other_entity.id</p>
</li>
</ol>
<p dir="auto">any other <code class="notranslate">query.something(SomeClass.m2o..something)</code> type requests should get put here.</p>