On Aug 19, 2007, at 7:22 AM, Gennady wrote:

>
> Hello,
>
> I'm a new sqlalchemy user. I have a question about generate id.
>
> I have a class with __init__ method, and after some initialization
> __init__ call events. Event handlers must know about id of new object.
>
> But if I use Sequence I don't know about new id in __init__ method
> before I flush changes to the database. And I can have two or more
> processes that connect to database. If I generate id in python may be
> a conflict on save objects in two processes.
>
> What is right way to resolve this problem? How to generate id and use
> transactions?
>
> Thank you.
>
> Gennady.


Gennady -

you can execute the sequence yourself and use that id explicitly, if  
you need to:

my_sequence = Sequence('my_sequence')
id = engine.execute(my_sequence)

Database sequences behave in such a manner that even if you call them  
from within a transaction, the ID counter increments, so theres no  
chance of a sequence ever returning the same ID twice.

If you want everything to occur within an ORM transaction, you can  
use a session in a transaction and execute your sequence using the  
trans (assuming 0.4):

sess = create_session()
sess.begin()
nextid = sess.execute(my_sequence)



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