Michael, thanks a lot for your reply. I haven't yet understood your 
explanation completely so please allow me to ask further.

Am Montag, 4. Mai 2009 23:01:01 schrieb Michael Bayer:
> the key to the problem is in the traceback:
>
> Traceback (most recent call last):
>   File "test.py", line 80, in <module>
>     item.logbookentries.append(logbookentry)
>   File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/attributes.py",
> line 159, in __get__
>     return self.impl.get(instance_state(instance))
>   File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/attributes.py",
> line 375, in get
>     value = callable_()
>   File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/strategies.py",
> line 568, in __call__
>     result = q.all()
>   File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/query.py", line
> 1193, in all
>     return list(self)
>   File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/query.py", line
> 1286, in __iter__
>     self.session._autoflush()
>   File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/session.py",
> line 899, in _autoflush
>     self.flush()
>   File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/session.py",
> line 1356, in flush
>     self._flush(objects)
>   File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/session.py",
> line 1413, in _flush
>     mapperutil.state_str(state), path))
> sqlalchemy.orm.exc.FlushError: Instance <LogbookEntry at 0xdeaa70> is an
> unsaved, pending instance and is an orphan (is not attached to any
> parent 'Item' instance via that classes' 'logbookentries' attribute)
>
> at the point of adding the object to item.logbookentries, autoflush is
> invoked.  You can see this in the stack trace that it is occuring before
> the append() ever happens.   Autoflush is attempting to flush your
> LogbookEntry which has been added to the session by attaching it to the
> "user" object.  Both "user" and "item" are already persistent so that's
> why you get "live" database activity when touching them.

Above the logbook thingy I run these statements:

        # Create a user...
        Session.save(user)
        # Create an item...
        Session.save(item)
        # Save the previously created objects into the database...
        Session.commit()

So my "Session.commit()" should do the database action and create one row 
for the user and one row for the item. So why is there a problem with the 
autoflushing? SQLAlchemy could save a new logbookentry to the database 
referring via foreign keys to the user and item rows in their respective 
tables. But the error message says that the LogbookEntry does not have any 
connection to an item. Why not? I'm at that very moment creating a 
connection by running

        item.logbookentries.append(logbookentry)

> The most straightforward way to prevent premature addition of your entry
> to the session via attachment to the "user" is to disable cascade on the
> "user.logbookentries" relation:
>
>         'logbookentries':orm.relation(LogbookEntry,
>             backref=orm.backref('user', uselist=False),
>             cascade="none"
>             ),
>
> this should be fine as you will always be associating a LogbookEntry
> with an item, which will take care of cascading it into the session.

Don't I need a cascade here? If the user of a certain logbook entry is 
removed then I need to set the logbook_table.user_id to None. Or don't I?

And my other question remains, too: why has this been working in 0.4?

Sorry for the followup but I want to learn why this is happening because 
I'm a big fan of SQLAlchemy and fear to fail at such relations in a bigger 
context. Thanks!

 Christoph

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to