getting last insert id .sqlalchemy orm

Posted by gummmibear on Stack Overflow See other posts from Stack Overflow or by gummmibear
Published on 2010-04-11T21:07:06Z Indexed on 2010/04/11 21:13 UTC
Read the original article Hit count: 412

Filed under:

Hi i use sqlalchemy, i need some help.

import hashlib
import sqlalchemy as sa
from sqlalchemy import orm

from allsun.model import meta


t_user =  sa.Table("users",meta.metadata,autoload=True)

class Duplicat(Exception):
 pass
class LoginExistsException(Exception):
 pass
class EmailExistsException(Exception):
 pass

class User(object):
 """
 def __setattr__(self, key, value):
  if key=='password' :
   value=unicode(hashlib.sha512(value).hexdigset())
   object.__setattr__(self,key,value)
"""
 def loginExists(self):
  try:
   meta.Session.query(User).filter(User.login==self.login).one()
  except orm.exc.NoResultFound:
   pass
  else:
   raise LoginExistsException()

 def emailExists(self):
  try:
   meta.Session.query(User).filter(User.email==self.email).one()
  except orm.exc.NoResultFound:
   pass
  else:
   raise EmailExistsException()


 def save(self):
  meta.Session.begin()
  meta.Session.save(self)
  try:
   meta.Session.commit()
  except sa.exc.IntegrityError:
   raise Duplicat()

How can i get inserted id when i call?

 user = User()
   user.login = request.params['login']
   user.password = hashlib.sha512(request.params['password']).hexdigest()
   user.email = request.params['email']
   user.save()

© Stack Overflow or respective owner

Related posts about sqlalchemy