I've been able to reduce my question to a simple example and thereby
found an answer:
(this is in a TurboGears project):

In the Model file:
class ItemType(Entity):
    id = Field(Integer, primary_key=True)
    name = Field(Unicode(16))
    sector = Field(Integer)
    weight = Field(Integer)
    size = Field(Integer)

In the Controller file:

    @expose(template="alchtest.templates.welcome")
    def simple(self):
        import time

        theItemType = ItemType(name=u"New Type", sector=0, weight=0,
size=0)
        session.flush()
        session.delete(theItemType)

        flash("New Type created and deleted!")
        return dict(now=time.ctime())

When I call http:../simple the object is created and by the end of the
controller method, is no longer in the database:

According to the SA (0.4.7p1) docs, the session.flush() call will not
effect any transactional session's ability to rollback, or commit. My
understanding is that Turbogears makes sure that each controller
method is in its own transaction.

Sorry for the messy first example. Writing this up helped me to solve
the problem.

On Aug 26, 12:04 pm, Jor Bratko <[EMAIL PROTECTED]> wrote:
> 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:
...
> 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