Hi,

  i'm trying to understand how to cope with errors from the database and I
  don't completely understand the following behaviour.

  I try to delete a Project that has integrity constrains, so correctly the
  db complains and SA raises an OperationalError. Right now I'm not trying
  to understand how to *prevent* this. I just want to understand how to give
  a message to the user and restore a working situation.

  [I'm working with postgreSQL and SA 5.0rc4]


     def delete_obj(obj):
         print "### deleting obj", repr(obj)
         try:
             session.delete(obj)
             print "session.delete:", session.deleted
             session.commit()
         except Exception, e:
             print "Problemi...", e.message
             session.rollback()

     q = session.query(Project)
     prj = q.filter_by(id=18).one()

     print repr(prj)
     delete_obj(prj)

     print "session.delete:", session.deleted
     for obj in session:
         print "obj", hex(id(obj)), type(obj)


     print "Project", prj  ###  <<<<<<<   this code raises the Interface error


   where is the problem? the connection (as I tend to believe) or the object 
'prj'?

   What's the state in which is now the object 'prj'? What should I do to
   go back to the previous situation?

   What exacltly means InterfaceError - connection already closed. I simply
   issued a .rollback(), why should it be closed? How should I test if a
   connection is closed? and how should I open it again?

   Thaks in advance
   sandro
   *:-)


#########  OUTPUT  ###########

<Project Project 1>
### deleting obj <Project Project 1>
session.delete: IdentitySet([<Project Project 1>])
Problemi... (OperationalError) ERROR:  update or delete on table 
"ticket_project" violates foreign key constraint 
"timereport_report_job_id_fkey" on table "timereport_report"
DETAIL:  Key (id)=(18) is still referenced from table "timereport_report".

session.delete: IdentitySet([])
obj 0x88a912c <class 'models.ticket.Project'>
obj 0x88b52cc <class 'models.ticket.Organization'>
obj 0x88b59ec <class 'models.cliente.User'>
No handlers could be found for logger "sqlalchemy.pool.QueuePool.0x...bc2c"
Project Traceback (most recent call last):
  File "test.py", line 24, in <module>
    print "Project", prj
  File "/home/misc/src/hg/py/sqlkit-pub/sqlkit/db/utils.py", line 65, in __str__
    format = get_description(self.__table__, attr='format')
  File "/home/misc/src/hg/py/sqlkit-pub/sqlkit/db/utils.py", line 226, in 
get_description
    return getattr(TableDescr(table), attr)
  File "/home/misc/src/hg/py/sqlkit-pub/sqlkit/db/utils.py", line 133, in 
__init__
    self.description, dbformat = self.guess_description()
  File "/home/misc/src/hg/py/sqlkit-pub/sqlkit/db/utils.py", line 197, in 
guess_description
    description, format = get_description_from_sqlkit(table)
  File "/home/misc/src/hg/py/sqlkit-pub/sqlkit/db/utils.py", line 240, in 
get_description_from_sqlkit
    res_proxy = metadata.bind.execute(SQL, table=table.name)
  File 
"/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py", line 
1202, in execute
    return connection.execute(statement, *multiparams, **params)
  File 
"/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py", line 
824, in execute
    return Connection.executors[c](self, object, multiparams, params)
  File 
"/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py", line 
888, in _execute_text
    return self.__execute_context(context)
  File 
"/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py", line 
896, in __execute_context
    self._cursor_execute(context.cursor, context.statement, 
context.parameters[0], context=context)
  File 
"/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py", line 
950, in _cursor_execute
    self._handle_dbapi_exception(e, statement, parameters, cursor, context)
  File 
"/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py", line 
928, in _handle_dbapi_exception
    self._autorollback()
  File 
"/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py", line 
794, in _autorollback
    self._rollback_impl()
  File 
"/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py", line 
738, in _rollback_impl
    self._handle_dbapi_exception(e, None, None, None, None)
  File 
"/home/misc/src/sqlalchemy/sqlalchemy-svn/lib/sqlalchemy/engine/base.py", line 
912, in _handle_dbapi_exception
    raise exc.DBAPIError.instance(None, None, e)
sqlalchemy.exc.InterfaceError: (InterfaceError) connection already closed None 
None

  
-- 
Sandro Dentella  *:-)
http://sqlkit.argolinux.org        SQLkit home page - PyGTK/python/sqlalchemy

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