Hello

I don't understand why the backref 'relation_b' of relationship 
'relation_a' is not properly set when declaring 'relation_a'. Below is an 
example that highlight this situation.
I must probably miss the philosophy being relation backref. What should I 
do to get backref working from the beginning?
 

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

Base = declarative_base()

_relation_a_table = Table('relation_a', Base.metadata,
        Column('id', Integer, primary_key=True)
    )

_relation_b_table = Table('relation_b', Base.metadata,
        Column('id', Integer, primary_key=True),
        ForeignKeyConstraint(['id'], ['relation_a.id'])
    )

class RelationA(Base):
    __table__ = _relation_a_table

class RelationB(Base):
    __table__ = _relation_b_table
    relation_a = relationship('RelationA', backref='relation_b')

if __name__ == '__main__':

    engine = create_engine('postgresql://xxxx@localhost:5432/xxxx')
    Session = sessionmaker(bind=engine)
    session = Session()

    print RelationA.__dict__.has_key('relation_b')

    q1 = session.query(RelationA).outerjoin(RelationA.relation_b)

    print RelationA.__dict__.has_key('relation_b')

    q2 = session.query(RelationA.id).outerjoin(RelationA.relation_b)

    # Output:
    # False
    # True

    # Output when q1 (line 31) is commented out:
    # False
    # False
    # Traceback (most recent call last):
    #  File "/my_path/dummy.py", line 35, in <module>
    #    q2 = session.query(RelationA.id).outerjoin(RelationA.relation_b)
    # AttributeError: type object 'RelationA' has no attribute 'relation_b'


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to