Knut Aksel Røysland wrote:
> 
> [snip]
> 
> However, an instance of D also needs a reference to an instance of C.
> If the appropriate instance of C exists in the database (or 
> is pending to go into it), I want to pick this one, or 
> otherwise create a new instance of C.
> 
> What I am looking for is the most clean way to achieve this. 
> I have run into trouble trying to use session.get(C, c_id) to 
> lookup instances of C that have not been flushed yet. (I 
> guess this might have something to do with primary keys not 
> working before instances have become persistent?)
> 
> Furthermore, I want the constructor of D to be where I lookup 
> or create the appropriate instance of P, which seems to 
> require that I pass the session object to the constructor so 
> it can use session.get to look for an existing instance of P. 
> I feel this passing of the session object around, is going to 
> clutter the code, so I am looking for a cleaner way.
> 
> [snip]

You may find the UniqueObject recipe useful:

http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueObject

It should help with the 'use-existing-or-create-new' part of your problem. I 
had a few difficulties when I tried to use it, but on reflection I think that 
was because I held references to objects after I had cleared the session. It 
also makes it difficult when you really do want to check whether an object 
exists in the DB without creating it, but it shouldn't be too difficult to 
adapt.

Instead of passing the session object around, you might be able to use the 
object_session function, which returns the session which the object is 
associated with, so in your constructor you could try 'session = 
object_session(self)'

Hope that helps,

Simon

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

Reply via email to