On Nov 12, 2010, at 7:32 PM, thatsanicehatyouh...@mac.com wrote:

> Hi,
> 
> I just want to check on something. Let's say I've got a script that's 
> populating a database and will commit the transaction at the end. It looks 
> for a particular object (let's call it "A"), and if NoResultFound it creates 
> a new object and does a session.add(A).
> 
> What if in a later iteration the script (before commit), I look up A again. 
> The query seems to not find the object in the database (of course),

mm, right there that's not the default behavior.  If you did an add(A), the 
next query() you do will autoflush.  "A" is now in the database within the 
scope of the current transaction, so query() will find it.


> but not the session either. The 'solution' is to keep track of new objects of 
> that type I create and look in that list before attempting to create a new 
> one. This doesn't seem elegant. Ideally I would have thought that a 
> session.query()... would have found the object newly added into the session. 
> Am I missing something or is there a more elegant way to handle this?

so...if you happen to have autoflush=False, turning that on would be your 
"elegant" switch.  Otherwise would need more detail.



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

Reply via email to