Yes Lance, now it works, thank you v.m. :-)
j
Lance Edgar wrote:
On 4/23/2010 9:19 AM, jose soares wrote:
jo wrote:
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

in my disperation, I tried also the following, but without success: :-(
from sqlalchemy.orm.session import Session
session=Session(autoflush=True,autocommit=True)

class Gruppo:
pass

mapper(Gruppo,
tbl['gruppo'],
column_prefix = 'gruppo_',
)



session.begin()
 > <sqlalchemy.orm.session.SessionTransaction object at 0x28a9710>

new=Gruppo( gruppo_id = 1, gruppo_descrizione='cat')
session.commit()
print Gruppo.get(1)

>SELECT gruppo.id AS gruppo_id, gruppo.descrizione AS gruppo_descrizione
 >FROM gruppo
 >WHERE gruppo.id = %(param_1)s

 >Col ('gruppo_id', 'gruppo_descrizione')
 >None

I don't understand what's wrong. I can't INSERT a new record into a table.
Could someone, give me some help?

Would this (not) work?

from sqlalchemy import *
from sqlalchemy.orm import mapper

metadata = MetaData()
groups = Table('groups', metadata, Column('id', Integer, primary_key=True), Column('name', String(25)))

class Group(object):
    pass

mapper(Group, groups)

from sqlalchemy.orm import sessionmaker
Session = sessionmaker()

session = Session()
group = Group()
group.name = 'cat'
session.add(group)
session.commit()

session.expunge_all()
group = session.query(Group).first()
print group.id
session.close()


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

Reply via email to