zrn*_*ely 6 python sqlalchemy pickle
我正在尝试使用 SqlAlchemy 在 postgresql 数据库中存储可变对象。对象的类大致为:
class Data(Mutable, object):
@classmethod
def coerce(cls, key, value):
return value
def __init__(self, foo, bar):
self._foo = foo # foo is an array of short strings
self._bar = bar # bar is a single short string
def append_to_foo(self, baz):
self._foo.append(baz)
self.changed()
# Various other methods for mutating/accessing foo and bar, which call self.changed()
# when they finish
Run Code Online (Sandbox Code Playgroud)
列定义为:
data = Column(Data.as_mutable(PickleType))
Run Code Online (Sandbox Code Playgroud)
每当我尝试向表中添加一行(包括此列)时,都会收到以下错误:
sqlalchemy.exc.StatementError: (builtins.AttributeError) Can't pickle local object 'WeakKeyDictionary.__init__.<locals>.remove' [SQL: "INSERT INTO table (id, user_id, data) VALUES (nextval('data_id_seq'), %(user_id)s, %(data)s) RETURNING data.id"] [parameters: [{'data': <mypkg.foo.Data object at 0x7f79b3b52c88>, 'user_id': 36}]]
Run Code Online (Sandbox Code Playgroud)
我已经确保Data类本身可以使用 Python 提示符进行 pickle;我可以pickle.dumps并且pickle.loads没有任何问题的实例。谷歌没有产生任何结果,我也找不到任何相关的错误报告。
我正在使用 SqlAlchemy 1.0.13 和 Python 3.5。
从阅读“支持酸洗”看来,您必须至少提供__getstate__自定义可变类型的方法:
这里开发人员的责任只是提供一种从 pickle 流中
__getstate__排除集合的方法:_parents()
这是因为可变扩展将 a 放置weakref.WeakKeyDictionary在值对象上,而该值对象无法被 pickle。__getstate__文档中提供了最小的实现:
class Data(Mutable, object):
...
def __getstate__(self):
d = self.__dict__.copy()
d.pop('_parents', None)
return d
Run Code Online (Sandbox Code Playgroud)
根据您的类型的实现方式,您可能__setstate__还必须提供。