I'm writing an application using Turbogears and I'm migrating it from
SQLObject to Elixir/SQLAlchemy. I have declared an Item class which
has an amount. I want the Item to be deleted if the amount goes to 0.

class Item(Entity):
    id = Field(Integer, primary_key=True)
    itemType = ManyToOne('ItemType')
    amount = Field(Integer)
    quality = Field(Integer)
    ...a bunch of ManyToOne relationships

   def SubtractAmount(self, amount):
       self.amount -= amount
       if self.amount == 0:
           self = None # was self.destroySelf() using SQLObject
       return

After anItem.SubtractAmount(anItem.amount) is called, I get the
following error:

Page handler: <bound method Root.ToonTrade of <mg1.controllers.Root
object at 0x01E6FF10>>
Traceback (most recent call last):
  File "c:\python25\lib\site-packages\cherrypy-2.3.0-py2.5.egg\cherrypy
\_cphttptools.py", line 121, in _run
    self.main()
  File "c:\python25\lib\site-packages\cherrypy-2.3.0-py2.5.egg\cherrypy
\_cphttptools.py", line 264, in main
    body = page_handler(*virtual_path, **self.params)
  File "<string>", line 3, in ToonTrade
  File "c:\python25\lib\site-packages\TurboGears-1.0.5-py2.5.egg
\turbogears\controllers.py", line 359, in expose
    *args, **kw)
  File "<string>", line 5, in run_with_transaction
  File "c:\python25\lib\site-packages\TurboGears-1.0.5-py2.5.egg
\turbogears\database.py", line 434, in sa_rwt
    session.commit()
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\orm\scoping.py", line 98, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\orm\session.py", line 557, in commit
    self.transaction.commit()
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\orm\session.py", line 262, in commit
    self._prepare_impl()
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\orm\session.py", line 246, in _prepare_impl
    self.session.flush()
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\orm\session.py", line 789, in flush
    self.uow.flush(self, objects)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\orm\unitofwork.py", line 233, in flush
    flush_context.execute()
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\orm\unitofwork.py", line 445, in execute
    UOWExecutor().execute(self, tasks)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\orm\unitofwork.py", line 930, in execute
    self.execute_save_steps(trans, task)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\orm\unitofwork.py", line 945, in execute_save_steps
    self.save_objects(trans, task)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\orm\unitofwork.py", line 936, in save_objects
    task.mapper._save_obj(task.polymorphic_tosave_objects, trans)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\orm\mapper.py", line 1161, in _save_obj
    c = connection.execute(statement.values(value_params), params)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\engine\base.py", line 844, in execute
    return Connection.executors[c](self, object, multiparams, params)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\engine\base.py", line 895, in execute_clauseelement
    return self._execute_compiled(elem.compile(dialect=self.dialect,
column_keys=keys, inline=len(params) > 1), distilled_params=params)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\engine\base.py", line 907, in _execute_compiled
    self.__execute_raw(context)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\engine\base.py", line 916, in __execute_raw
    self._cursor_execute(context.cursor, context.statement,
context.parameters[0], context=context)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\engine\base.py", line 960, in _cursor_execute
    self._handle_dbapi_exception(e, statement, parameters, cursor)
  File "c:\python25\lib\site-packages\SQLAlchemy-0.4.7-py2.5.egg
\sqlalchemy\engine\base.py", line 942, in _handle_dbapi_exception
    raise exceptions.DBAPIError.instance(statement, parameters, e,
connection_invalidated=is_disconnect)
InterfaceError: (InterfaceError) Error binding parameter 1 - probably
unsupported type. u'INSERT INTO mg1_model_item ("itemType_id", amount,
quality, owner_id, recipe_id, offered_id, requested_id, proceeds_id,
rawmaterial_id, production_id, tool_id, shop_id) VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' [0, Decimal("0"), 0, 6, None,
None, None, None, None, None, None, None]


This looks like Elixir/SA is trying to insert the Item object with an
amount column of 0, which is not the desired outcome. (Is Decimal("0")
not supported by SQLite?)
One other complication is that it is possible that SubtractAmount() is
called on an Item which was created during this controller invocation.
Since I think Turbogears uses a ScopedSession for each controller
invocation, I don't explicitly call save() or session.save() on the
newly created items because I thought the ScopedSession took care of
that for me.

I've searched the docs and other messages on this board and can't
figure out what I'm doing wrong. The only example of deleting an
object (and therefore its row(s) in the database) is to either set an
object reference to None, or use Python's "del" statement  (which
yields the same error).
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" 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/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to