1. Michael Bayer
  2. sqlalchemy
  3. Issues


Issue #2989 duplicate

Query oerder_by with relationship as parameter uses the according join condition in the order by clause

Ronny Pfannschmidt
created an issue

this problem has shown up when porting to oracle, which reported a random error uppon seeing the sql

after inspection i was sure the code was certainly wrong - at least the join condition makes absolutely no sense there

imho it should be an error to pass a relationship there

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)

results in

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

Comments (3)

  1. Log in to comment