Backref confusion

36 views
Skip to first unread message

Bobby Impollonia

unread,
Nov 13, 2012, 11:03:03 PM11/13/12
to sqlal...@googlegroups.com
I have constructed a sample program consisting of two mapped classes (using sqlalchemy.ext.declarative) that have a relationship/ backref between them. At runtime the program does the following:
1) Print whether the parent class has an attribute for its relationship to the child (declared as the backref)
2) Construct a child object
3) Repeat step 1

The result (with SQLA 0.7.9) is that it prints 'False' during step 1 and then 'True' during step 3. I would expect True to be printed both times.

Here is the full source of the program:

Why does the property not exist when the first print statement executes?

Thanks for any guidance.

Michael Bayer

unread,
Nov 14, 2012, 12:16:42 AM11/14/12
to sqlal...@googlegroups.com
"addresses" is generated on the Person class when the mappers enter the "configuration" step, which is an automatically invoked process which occurs when a mapping is first used.   this process is deferred until a point at which it's safe to assume all mappings are present, so that relationship() directives, which refer to other mappings, can proceed to reconcile the mappings they point to - otherwise by definition one of the mappings/classes (if using declarative) doesn't exist yet for relationship/backref.

the process can be manually invoked via configure_mappers():

if __name__ == '__main__':
    from sqlalchemy.orm import configure_mappers
    configure_mappers()
    print hasattr(Person, 'addresses')
    Address()
    print hasattr(Person, 'addresses')




Thanks for any guidance.

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/SRZzsLt7qb0J.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Bobby Impollonia

unread,
Nov 14, 2012, 1:03:02 AM11/14/12
to sqlal...@googlegroups.com
Thank you! That is exactly the function I needed.
Reply all
Reply to author
Forward
0 new messages