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

or it can be download here : http://utilitybase.com/paste/15927

this is the trace back

Traceback (most recent call last):
  File "I:\workspace\radio\radiosite\new_website\radioweb\radioweb
\model\show_bug.py", line 25, in <module>
    coolSite = Site(user=victor, title=u'Cool site')
  File "c:\python25\lib\site-packages\Elixir-0.6.1-py2.5.egg\elixir
\entity.py", line 718, in __call__
    return type.__call__(cls, *args, **kwargs)
  File "<string>", line 4, in __init__
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\attributes.py",
line 890, in initialize_instance
    return manager.events.original_init(*mixed[1:], **kwargs)
  File "c:\python25\lib\site-packages\Elixir-0.6.1-py2.5.egg\elixir
\entity.py", line 876, in __init__
    self.set(**kwargs)
  File "c:\python25\lib\site-packages\Elixir-0.6.1-py2.5.egg\elixir
\entity.py", line 880, in set
    setattr(self, key, value)
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\attributes.py",
line 151, in __set__
    self.impl.set(instance_state(instance), value, None)
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\attributes.py",
line 557, in set
    value = self.fire_replace_event(state, value, old, initiator)
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\attributes.py",
line 577, in fire_replace_event
    value = ext.set(state, value, previous, initiator or self)
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\attributes.py",
line 810, in set
    new_state.get_impl(self.key).append(new_state, state.obj(),
initiator, passive=PASSIVE_NO_CALLABLES)
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\attributes.py",
line 387, in append
    self.set(state, value, initiator)
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\attributes.py",
line 556, in set
    old = self.get(state)
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\attributes.py",
line 375, in get
    value = callable_()
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\strategies.py",
line 589, in __call__
    result = q.all()
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\query.py", line
1193, in all
    return list(self)
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\query.py", line
1286, in __iter__
    self.session._autoflush()
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\session.py", line
899, in _autoflush
    self.flush()
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\session.py", line
1351, in flush
    self._flush(objects)
  File "C:\Python25\Lib\site-packages\sqlalchemy\orm\session.py", line
1408, in _flush
    mapperutil.state_str(state), path))
sqlalchemy.orm.exc.FlushError: Instance <Site at 0x10ad310> is an
unsaved, pending instance and is an orphan (is not attached to any
parent 'User' instance via that classes' 'site' attribute)

It is so strange, why it said that my site is a orphan? It seems that
there is something behind that method invoke autoflush, but at this
moment, the site is not attached to user yet, so here comes the
problem?

The version of sqlalchemy is 5.5, and the version of elixir is 0.6.1.
I don't understand, the user is already there in memory, what will
invoke a autoflush? Is that a bug of sqlalchemy?
It is an unreasonable behavior sqlalchemy did, it stop me from add any
site to my user. How can I solve this problem?

Thanks.
Victor Lin.
--~--~---------~--~----~------------~-------~--~----~
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