On Mar 23, 2011, at 11:28 AM, [email protected] wrote:

> Hello everyone,
> 
> I am currently working on a Pylons project, and keep on receiving the
> following error from SQLAlchemy:
> 
> InvalidRequestError: stale association proxy, parent object has gone
> out of scope

this is because the parent is being garbage collected while the association 
proxy is still doing its work:

# will not work with immediate gc
for item in session.query(Order).first().items:
    print item

the solution is to do it like this:

o1 = session.query(Order).first()
for item in o1.items:
    print item


example is attached.






> 
> Here is the code for the model:
> 
> class AccountInfo(Base):
>    __tablename__ = 'account_info'
> 
>    userid = schema.Column(types.Integer,
>        schema.ForeignKey("users.id"), nullable=False, primary_key=True)
>    enabled = schema.Column(types.Integer, default=1)
>    username = schema.Column(types.Unicode(25), nullable=False,unique=True)
>    password = schema.Column(types.Unicode(255), default=u'password')
>    #leveled_privs = orm.relation("PrivilegeLevel",
> backref="account_info",
> collection_class=attribute_mapped_collection('priv_id'))
>    # Below creates a relationship between AccountInfo and
> PrivilegeLevel placed into a dict sorted by priv_id
>    perms = orm.relation("Privilege", secondary=privileges_assoc)#,
> backref="account_info")
>    privileges = association_proxy("perms","name")
>    def __init__(self,info):
>        for k,v in info:
>            setattr(self, k, v)
> 
> "privileges" is the source of the problem. Whenever I try to use it in
> the site, I get the previously mentioned error. I've taken a look at
> the documentation on the site, and I believe that I am using it
> correctly.
> 
> When I searched google, I wasn't able to find anything useful. I did
> find something about removing stale cached attribute instances here:
> http://www.mail-archive.com/[email protected]/msg08914.html
> 
> But I couldn't find any attributes on my instances that seemed similar
> to what that user was speaking about.
> 
> Here's an example of how I'm using it, if that helps:
> 
>> "edit.perms" in myAccountInfoInstance.privileges
> True
> 
> In the Python shell, it works occasionally. In a request, not at all
> 
> Is anyone able to help me?
> 
> Cody
> 
> -- 
> 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.
> 

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy

Base = declarative_base()

class Order(Base):
    __tablename__ = 'order'

    order_id = Column(Integer, primary_key=True)
    order_items = relationship("OrderItem",)
    items = association_proxy("order_items", "item")

class Item(Base):
    __tablename__ = 'item'
    item_id = Column(Integer, primary_key=True)

class OrderItem(Base):
    __tablename__ = 'orderitem'
    order_id = Column(Integer, ForeignKey('order.order_id'), primary_key=True)
    item_id = Column(Integer, ForeignKey('item.item_id'), primary_key=True)
    price = Column(Float, nullable=False)

    item = relationship(Item)

engine = create_engine('sqlite://')
Base.metadata.create_all(engine)

session = Session(engine)

i1, i2, i3 = Item(), Item(), Item()
o1 = Order(order_items=[
    OrderItem(item=i1, price=5),
    OrderItem(item=i2, price=20),
    OrderItem(item=i3, price=10),
])

session.add(o1)
del o1

# will work
o1 = session.query(Order).first()
for item in o1.items:
    print item

del o1

# will not work with immediate gc
for item in session.query(Order).first().items:
    print item

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