Alex Grönholm
unread,Feb 15, 2012, 6:52:38 PM2/15/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to sqlal...@googlegroups.com
I'm having trouble unpickling model instances where the class has an attribute-mapped collection:
import pickle
from sqlalchemy import create_engine, Column, Integer, Unicode, ForeignKey
from sqlalchemy.orm import Session, relationship
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
engine = create_engine('sqlite:///')
session = Session(engine)
class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, primary_key=True)
children = relationship('Child', cascade='all, delete-orphan', lazy='subquery', collection_class=attribute_mapped_collection('key'))
class Child(Base):
__tablename__ = 'children'
parent_id = Column(Integer, ForeignKey(Parent.id), primary_key=True)
key = Column(Integer, primary_key=True)
value = Column(Unicode)
Base.metadata.create_all(engine)
parent = Parent()
session.add(parent)
session.flush()
session.refresh(parent)
pickled = pickle.dumps(parent, 2)
pickle.loads(pickled)
Executing this results in:
Traceback (most recent call last):
File "sqlatest.py", line 32, in <module>
pickle.loads(pickled)
File "/usr/lib/python2.7/pickle.py", line 1382, in loads
return Unpickler(file).load()
File "/usr/lib/python2.7/pickle.py", line 858, in load
dispatch[key](self)
File "/usr/lib/python2.7/pickle.py", line 1083, in load_newobj
obj = cls.__new__(cls, *args)
TypeError: attrgetter expected 1 arguments, got 0
If I remove the relationship, pickling and unpickling work fine. None of the other mapped collection variants work either, though they produce different tracebacks. Is this a bug or am I doing something wrong?