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.

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.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to