Mike, thanks for your reply.

> what happens if you just leave "_state" alone ?  there shouldnt be any
> need to mess with _state (nor _entity_name).   the only attribute
> worth deleting for the cache operation is "_sa_session_id" so that the
> instance isnt associated with any particular session when it gets
> cached.  Id also consider using session.merge(dont_load=True) which is
> designed for use with caches (and also watch out for that log.debug(),
> debug() calls using the standard logging module are notoriously slow).

The reason for deleting _state is to save some space in cache. I save
instances to cache on "get" operation, so they are unmodified. But, of
course, it is internal thing so the final decision is yours :)

I gave up trying merge(dont_load=True) after running this sample:

users = Table('users', metadata,
              Column('id', Integer, primary_key=True),
              Column('name', String(100)),
              Column('surname', String(100)))

mapper(User, users,
       properties={
        'surname': deferred(users.c.surname)
        })

s = create_session()
u = User()
u.name = 'anton'
u.surname = 'belyaev'
s.save(u)
s.flush()

# now we need an instance with not loaded surname (because it is
deferred)
s = create_session()
u = s.query(User).get(1)

# cache it
cache = pickle.dumps(u)

# try to restore in a new session
s = create_session()
u = pickle.loads(cache)
u = s.merge(u, dont_load=True)

The latest statement fails with:

File "/home/anton/eggs/lib/python2.5/site-packages/SQLAlchemy-0.4.1-
py2.5.egg/sqlalchemy/orm/session.py", line 1136, in object_session
    if obj in sess:
TypeError: argument of type 'NoneType' is not iterable

Some notes on this test case:
1) If "surname" was a simple (not deferred) column property, merge
would work fine.
2) session.update(u) instead of merge would work fine even with
deferred column property, and the property itself would work fine (it
would load on first reference).

> the only trac ticket for this is #490, which with our current
> extension architecture is pretty easy to fix so its resolved in 3967 -
> MapperExtensions are now fully inherited.  If you apply the same
> MapperExtension explicitly to a base mapper and a subclass mapper,
> using the same ME instance will have the effect of it being applied
> only once (and using two different ME instances will have the effect
> of both being applied to the subclass separately).

I meant #870 (sorry, I should had provided reference in the first
message).

Back again to the testcase and note 2:

If, let say, I had some inheritance:

class Teacher(User):
    pass

with polymorphic_fetch='deferred' (this is important), even
session.update(u) would not work. Because in this case deferred
attributes work through callables in _state, and callable does not
survive pickling.

Thanks.
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to