expression-level many-to-one comparison

Issue #2797 resolved
Mike Bayer repo owner created an issue

Just as obj.many_to_one == some_object works, it should do the right thing if you say obj.many_to_one == SomeClass, which is actually longhand notation for the "primaryjoin" but is consistent in other systems.

this is in 0.9 so it gets noticed but should be OK for a backport to 0.8.

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)


class B(Base):
    __tablename__ = 'b'

    id = Column(Integer, primary_key=True)
    a_id = Column(Integer, ForeignKey('a.id'))
    a = relationship("A")

s = Session()

# support this
# q1 = s.query(B).filter(B.a == A).exists()

# checks the argument, then just does this:
q1 = s.query(B).filter(B.a.expression).exists()

print s.query(A).filter(q1)

Comments (3)

  1. Log in to comment