when calling upon backref-bound attributes immediately after constructing 
mappers, the mappers have not yet had any cue to reconcile their inter-mapper 
linkages:

class A(..):
   x = relationship(B, backref="foo")

class B(..):
  ...

B.foo  <--- attribute error

"B.foo" doesn't exist yet, A.x couldn't apply "foo" because "B" didn't exist 
yet.

any operation on A or B, or anything else that's mapped, will reconcile this.

or, when your mappings are first completed, call this:

from sqlalchemy.orm import configure_mappers
configure_mappers()

can be called any number of times.





On Apr 10, 2013, at 11:02 AM, Etienne Rouxel <[email protected]> wrote:

> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

-- 
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 [email protected].
To post to this group, send email to [email protected].
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