On Wed, Apr 30, 2008 at 9:44 PM, Michael Bayer <[EMAIL PROTECTED]> wrote:
>
>
>  On Apr 30, 2008, at 3:23 PM, Eric Lemoine wrote:
>
>  >
>  > Yes, the sequence is my table's PK. What I want to know is the PK
>  > value of the line I'm going to insert (or I've just inserted). So I
>  > guess this is indeed INSERT RETURNING.
>
>  if you pre-execute the sequence, the number you get back from it is
>  yours to keep and will never come up again (unless the sequence is
>  manually manipulated).   Its safe to use for a primary key value at
>  any time regardless of concurrent threads which also use that sequence.
>
>  SQLAlchemy does this process for you automatically, so if you just set
>  the Sequence() on your table's primary key Column, you can safely save
>  and flush your instances without assigning any identifier, and the
>  newly generated id is present on the corresponding class attributes,
>  i.e.:
>
>  mytable = Table('mytable', metadata, Column('id', Sequence('my_seq'),
>  primary_key=True), ...)
>
>  mapper(MyClass, mytable, ...)
>
>  x = MyClass()
>  session.save(x)
>  session.flush()
>
>  newly_inserted_id = x.id
>
>  no threading issues to worry about.   Same thing happens with raw
>  inserts, if you leave the "id" column out of the values list:
>
>  result = engine.execute(mytable.insert())
>  newly_inserted_id = result.last_inserted_ids()[0]

Great! Thanks,

--
Eric

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