On Mon, 2010-12-20 at 10:16 -0500, Michael Bayer wrote:
> On Dec 20, 2010, at 3:42 AM, Torsten Landschoff wrote:

> > INSERT INTO group_items (group_id, item_id, item_order) VALUES (?, ?, ?)
> > (None, None, None)
> > 
> > Both the group and the item where created correctly so I ended up with a
> > database which has all the data, just missing their relationships ;-)
> > 
> > 
> > I reverted to running the query when first connecting to the database.
> > What I would like to know: Is it officially impossible to run queries
> > from session events? What session functionality is usable from within
> > session events? (I know that session.{dirty, new, deleted} works fine).
> 
> I can't think of anything that would go wrong from running a Query
> inside of the events - the flush process itself runs queries.   Also
> in the before_flush() event you can also change the state of the
> session to any degree.

Good to know, thanks!

> In this case, we have another verb besides "Query", which is
> "replace", as in "replace a None reference".   That sounds like the
> thing you're doing that changes state, likely in an unexpected way
> based on your description.   Would need to understand what you mean by

The part of replacing the None reference still happens with preloading
and seems to be okay now.

Basically I have a field "format" attached to the item that is inserted.
This is a relation (via Column "format_id") into a data format table.
Most of the time, the data will be in the default format which is
therefore automatically set if not overridden.

So in the after_attach event, I set the format relation to the default
format in the database.

Now this is done via

class MySession(Session):
    ...
    def __init__(self, ...):
        Session.__init__(...)
        self._format, = self.query(DataFormat).filter(...)
    @property
    def default_format(self):
        return self._format

In after_attach:

    ...
    if instance.format is None:
        instance.format = session.default_format

I would like to have the database supply the default value, but is is
user-configurable. Interestingly, in any case the format is set
correctly. But the group_item table update is broken.

>  "replace" here and under what codepath that's occurring.   Under a
> normal "attach" event via a userland "add()" it should be OK.

So that might be the problem then. Attaching the instance to the session
works by the save-update cascade: Items are added to group.children
which models the group_items relation. I assumed that adding entries to
that relation will cause an add() and therefore the after_attach
callback. Is after_attach only called when flushing?

Greetings, Torsten

-- 
DYNAmore Gesellschaft fuer Ingenieurdienstleistungen mbH
Torsten Landschoff

Office Dresden
Tel: +49-(0)351-4519587
Fax: +49-(0)351-4519561

mailto:[email protected]
http://www.dynamore.de

Registration court: Mannheim, HRB: 109659, based in Karlsruhe,
Managing director:  Prof. Dr. K. Schweizerhof, Dipl.-Math. U. Franz

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