On May 28, 2007, at 2:17 AM, Shannon -jj Behrens wrote:

>
> On 5/27/07, Anil <[EMAIL PROTECTED]> wrote:
>> user_mapper = assign_mapper(ctx, User, user_table,
>>                             properties = {
>>                                           'alerts': relation(Alert,
>> cascade="all, delete-orphan")
>>                                           })
>> alert_mapper = assign_mapper(ctx, Alert, alert_table)
>>
>>
>> user = session["user"]
>> alert = model.Alert(form_result["name"])
>> user.alerts.append(alert)
>> user.save_or_update()
>> user.flush()
>>
>>
>> I save a User SA object in session["user"] in another page. Then, in
>> this page, I try to add Alert objects to the user object. I get this
>> exception.
>>
>> <class 'sqlalchemy.exceptions.InvalidRequestError'>: Object
>> '<entic.models.Alert object at 0xcc2970>' is already attached to
>> session '13378672' (this is '12302224')
>>
>> Can someone give me pointers on how I can resolve this?
>>
>> Plus, are there any best practices for passing around SA mapped
>> objects in beaker sessions?
>
> I would say *don't* ;)
>
> I wouldn't expect a SA object to be serializable.  It just doesn't
> make sense to me.  I don't even want to think about complications with
> the database and ACID, nor do I want to consider the scalability
> concerns (the SA object should be tied to a particular SA session,
> right?).
>

SA objects are serializable (as long as you arent using assign_mapper 
(), which can complicate things unless you define a custom  
__getstate__() method).  the error above is because the entity is not  
being detached from its original session.  if you are going to  
serialize, you have to manually shuttle the object to and from the  
appropriate sessions.

three ways to get an object out of serialization and back into an SA  
session are:

1. mapped class has a __getstate__() that only copies desired  
properties (and wont copy SA session pointers):

     beaker.put(key, obj)
     ...
     obj  = beaker.get(key)
     session.save_or_update(obj)

2. regular old mapped class.  add an expunge() step.

     session.expunge(obj)
     beaker.put(key, obj)
     ...
     obj  = beaker.get(key)
     session.save_or_update(obj)

3. dont worry about __getstate__() or expunge() on the original  
object, use merge().  this is "cleaner" than the expunge() method  
above, but will usually force a load of the object from the database  
and therefore is not necessarily as "efficient", also it copies the  
state of the given object to the target object which may be error-prone.

    beaker.put(key, obj)
    ...
    obj = beaker.get(key)
    obj = session.merge(obj)



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" 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/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to