Mariano Mara wrote:
Excerpts from jo's message of Fri Apr 23 03:16:21 -0300 2010:
Hi all,
I need to insert a new row and get back the last inserted id,
I have some difficulty using the flush(), then I'm trying with commit() but
I can't understand how commit() works in 0.6.
In the following script I try to update a row and it works properly
but when I try to insert a new one, it doesn't work,
there's no messages but the row is not inserted into the table.
Is this the right way ?
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(autoflush=True)
session = Session()
#update an existing row... it works
old = Specie.get('D')
old.specie_descrizione='dog'
#insert a new row... it doesn't work
new=Specie(
specie_codice='C',
specie_descrizione='cat'
)
session.commit()
thanks for any help
j
You forgot to add the instance to the session before the commit. See
http://www.sqlalchemy.org/docs/ormtutorial.html#adding-new-objects
Thank you Marionao for replay to my question.
I tried as you suggest, but now it raises an InvalidRequestError, take a
look:
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(autoflush=True)
session = Session()
#insert a new row... it doesn't work
new=Specie(
specie_codice='C',
specie_descrizione='cat'
)
session.add(new)
session.commit()
InvalidRequestError: Object '<Specie at 0x14ca650>' is already attached
to session '54658512' (this is '21800720')
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.