[Sqlalchemy-tickets] Issue #3029: fully self equated join condition? (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
|
From: Mike B. <iss...@bi...> - 2014-04-19 20:00:25
|
New issue 3029: fully self equated join condition? https://bitbucket.org/zzzeek/sqlalchemy/issue/3029/fully-self-equated-join-condition Mike Bayer: ``` #!python from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class Category(Base): __tablename__ = 'categories' id = Column(SmallInteger, primary_key=True) path = Column(String, unique=True, nullable=False) all_subcats = relationship('Category', viewonly=True, primaryjoin=path == remote(foreign(path)).concat('%') ) print Session().query(Category).options(joinedload("all_subcats")) ``` patch: ``` #!diff diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py index 311fba4..ed0a9a5 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -2405,6 +2405,10 @@ class JoinCondition(object): self.direction = ONETOMANY elif manytoone_local and not onetomany_local: self.direction = MANYTOONE + elif self_equated.intersection(self.foreign_key_columns): + self.direction = ONETOMANY + elif self_equated and not self_equated.intersection(self.foreign_key_columns): + self.direction = MANYTOONE else: raise sa_exc.ArgumentError( "Can't determine relationship" ``` |