Victor Lin wrote:
>
> Hi,
>
> I am writing a turbogears2 application, with elixir. I got a problem
> when I try to create an new entity and attach it to another entity. I
> create a simple program to repruduce the problem:
>
> from elixir import *
>
> class User(Entity):
>     name = Field(Unicode)
>     site = OneToOne('Site', cascade="all, delete-orphan")
>
> class Site(Entity):
>     user = ManyToOne('User')
>     title = Field(Unicode)
>
> metadata.bind = 'sqlite:///'
> setup_all(True)
>
> # we do create a new user victor here
> victor = User(name=u'victor')
> session.commit()
>
> # and we create a site for victor
> coolSite = Site(user=victor, title=u'Cool site')
> session.commit()
>

elixir is adding the "Site" object to the session before the "user=victor"
attribute is set.   The setting of user=victor in turn is triggering an
attribute operation on User.site, which is a result of the bidirectional
reference between User and Site.   All attributes on User have been
expired since you called session.commit(), and upon next access will be
re-loaded from the database.

If User.sites were one-to-many (more common), SQLA would be stashing the
value in a temporary collection and would not be triggering the load,
however in the case of a scalar reference (User.site), it triggers a load
in order to create a history event.  Since Site is already in the session,
the session first flushes any pending changes before loading, and the
error is raised.

It is a TODO within SQLAlchemy for this particular operation to not
trigger an unnecessary load.

However, the biggest issue here is that Elixir is automatically adding
User to the Session, which is a behavior SQLAlchemy has been discouraging
for a couple of years now as it leads to more than one scenario where this
kind of thing happens, so you should check on the Elixir list for how to
disable this.

As far as the loading and the flushing is concerned, that is the result of
the session's expire_on_commit and autoflush behavior both of which are
described in the session documentation.

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