>> 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.

Ahh, but session.save() was already called, so trying to fetch a 
database-generated attribute (such as the primary key in my case) should 
trigger a flush of the row itself. That can be done with any database. 
It wouldn't be done on __init__, nor would it be done on save(). It 
would be done only once you tried to fetch the id property (only for 
objects in the Pending state)

Okay, as an example. Let's say you have:

something_table = Table('something', metadata,
        Column('id', Integer, primary_key=True),
        Column('name', String)
)

class Something(object):
        def __init__(self,name):
                self.name = name

        def __repr__(self):
                return "<Something(%d,'%s')>" % (self.id, self.name)

mapper(Something,something_table)

obj = Something('blah')
session.save(obj)
print "Look ma, a something: %s" % obj

In theory that will throw an exception since Something's __repr__ will 
have None for the id property, since id was never retrieved.

(if I just did:

obj = Something('blah')
print "Bad idea: %s" % obj

then I would expect an exception, since it's not saved)

Does this description make more sense that what I said before?

-Adam Batkin

--~--~---------~--~----~------------~-------~--~----~
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