On Wed, Apr 30, 2008 at 9:23 PM, Eric Lemoine <[EMAIL PROTECTED]> wrote:
> On Wed, Apr 30, 2008 at 9:02 PM, Michael Bayer <[EMAIL PROTECTED]> wrote:
>  >
>  >
>  >  On Apr 30, 2008, at 12:56 PM, Eric Lemoine wrote:
>  >
>  >  >
>  >  > thread 1                                                     thread 2
>  >  > execute(seq) -> nextid = n
>  >  >
>  >  > execute(seq) -> nextid = n
>  >  > model.Session.save(campfacility)
>  >  >
>  >  > model.Session.save(campfacility) -> BUG, nextid isn't correct
>  >
>  >  whats "correct" here, you'd like the integer identifier to be in exact
>  >  row-insert order ?   if the column is a non-primary key column, the
>  >  sequence will be executed "inline" within the executed SQL so that it
>  >  will in fact be in row insert order (i.e. update table set
>  >  foo_id=nextval(myseq)).  you can also do this at flush time by
>  >  assigning "func.nextval(literal_column("my_sequence_name"))" to the
>  >  mapped attribute (assuming its not a PK).
>  >
>  >  for primary keys we need to know the ID beforehand in most cases since
>  >  PG historically has not had a way to get that ID back nicely after
>  >  insert (it has INSERT RETURNING now but we haven't standardized on
>  >  that yet).
>
>  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.

With psycopg2, I know people using this:

sql = "INSERT INTO \"%s\" (%s) VALUES (%s)" % (self.table, columns, values)
cursor = db.cursor()
cursor.execute(str(sql), values)
cursor.execute("SELECT currval('%s');" % sequence_name)
id = cursor.fetchone()[0]
self.db.commit()

I'm wondering if this is safe. And if so, if there's a way to do the
same with SA.

Thanks,

--
Eric

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