On Dec 9, 2007, at 4:10 PM, Adam Batkin wrote:

>
> If I create an object, then save() it, potentially the object won't be
> actually persisted until sqlalchemy decides that it needs to (for
> example on flush/commit, or when some query involving Thing's table  
> gets
> executed) which is good. But (in my opinion) the lazyness is a bit too
> lazy when it comes to autogenerated primary keys:
>
> t = Something('foo')
> session.save(t)
> assert t.id is None
>
> but if I then:
>
> session.flush()
> assert t.id is not None

whats the issue there?  a lot of attributes get populated after flush,  
not just primary key attributes but also foreign key-holding  
attributes.   if you need the PK because you're trying to link up  
related objects manually on their foreign keys, relation() takes care  
of that for you when using the ORM.  (but if you choose, you can  
autogenerate the ID yourself if its possible with your database).

>
>
> My thought is that sqlalchemy should force the object to be flushed  
> (or
> whatever must be done to determine the ID, possibly just selecting the
> next value from a sequence) when the id property is retrieved.
>

can't be done for mysql, sqlite, MSSQL, others, without issuing an  
INSERT.  you cant INSERT on __init__ since not every attribute may be  
populated on the object, and additionally our session doesnt generally  
like to do things "automatically", with the exception of the  
"autoflush" feature.   also we don't emit any modifying SQL externally  
to the flush.  if youre using a database like postgres or oracle,  
you're free to execute the sequence yourself and apply the new value  
to the primary key attribute of your object, and it will be used as  
the primary key value when the INSERT does actually occur.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
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