On Oct 27, 2013, at 4:00 PM, Jordan Rein <[email protected]> wrote:
> > rd4, one of the responders in the Reddit thread, gave me a decent idea of > what's going on with this code, but I'm still hungry for a more complete > answer. The key thing I want to learn is the order of events that take place > in this code, because it seems like I have some misconceptions about how the > relationship() function actually works. I'll be checking this thread and the > Reddit thread throughout the day - if you've got some extra time, please > consider answering! > > As I just noted in a different response, the best way to figure out what’s going on is to watch the SQL being emitted, which is most expedient by using echo=True with create_engine. If you take a Node and attach another Node to the collection, then call session.flush() - watch what the SQL does. Load a Node with a Query, then access the right_nodes collection, watch again the SQL being emitted. A good way to step through a program slowly is to use pdb, or a similar tool. If you followed your mapper setup with a script like this: engine = create_engine(…, echo=True) session = Session(engine) import pdb pdb.set_trace() n1 = Node() session.add(n1) session.flush() n1.add_node(Node()) session.flush() the “pdb.set_trace()” will drop you into a prompt. you type “next” to move to the next line in the immediate script, so you can see what each step does. There’s a lot of other things you can do with pdb in order to learn how some code works, like setting a breakpoint and using “where” to see what the current stack trace is, stuff like that. more nuts and bolts, here’s how the “id” gets turned into a number when it loads the related nodes: some_node = Node(id=1) from sqlalchemy import inspect lazy_clause = Node.right_nodes.property.strategy.lazy_clause(inspect(some_node)) print lazy_clause print lazy_clause.compile().params you’d need to read through the LazyLoader.lazy_clause() method in sqlalchemy/orm/strategies.py to see what’s involved in making that happen. you could even pdb through each step of the lazy_clause() method and poke around. Depends on how deeply you really want to know how it works. There’s also things I’ve written/presented which describe the persistence process - you can read section 20.9, “Unit of Work” in my AOSA chapter on SQLAlchemy: http://aosabook.org/en/sqlalchemy.html two talks that discuss how the session works - SQLAlchemy, an Architectural Retrospective http://blip.tv/pygotham/sqlalchemy-an-architectural-retrospective-5583765 and the SQLAlchemy Session in Depth http://techspot.zzzeek.org/2012/11/14/pycon-canada-the-sqlalchemy-session-in-depth/ . > > -- > 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. > 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. For more options, visit https://groups.google.com/groups/opt_out.
