0

我正在转换一个库以使用SQLAlchemy作为数据存储。我喜欢 PickleType 列的灵活性,但它在酸洗 SA 对象(表行)时似乎效果不佳。即使我在 unpickling 时重载 setstate 和 getstate 以进行查询+会话合并,也没有跨越该 pickle 边界的引用完整性。这意味着我无法查询对象集合。

class Bar(Base):
    id = Column(Integer, primary_key=True)
    __tablename__ = 'bars'
    foo_id = Column(Integer, ForeignKey('foos.id'), primary_key=True)

class Foo(Base):
    __tablename__ = 'foos'
    values = Column(PickleType)
    #values = relationship(Bar)  # list interface (one->many), but can't assign a scalar or use a dictionary
    def __init__(self):
        self.values = [Bar(), Bar()]

        # only allowed with PickleType column
        #self.values = Bar()
        #self.values = {'one' : Bar()}
        #self.values = [ [Bar(), Bar()], [Bar(), Bar()]]

# get all Foo's with a Bar whose id=1
session.query(Foo).filter(Foo.values.any(Bar.id == 1)).all()

一种解决方法是实现我自己的可变对象类型,如此处所示。我想象有某种扁平化方案,它遍历集合并将它们附加到更简单的 one->many 关系。也许扁平列表可能必须是腌制集合对象的弱引用?

跟踪更改和引用听起来并不有趣,而且我在其他任何地方都找不到任何人腌制 SA 行的例子(也许表明我的设计不好?)。有什么建议吗?

编辑1:经过一些讨论,我简化了请求。我正在寻找一个可以作为标量或集合的单一属性。这是我的(失败的)尝试:

from sqlalchemy import MetaData, Column, Integer, PickleType, String, ForeignKey, create_engine
from sqlalchemy.orm import relationship, Session
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.collections import attribute_mapped_collection


# from http://www.sqlalchemy.org/trac/browser/examples/vertical
from sqlalchemy_examples.vertical import dictlike_polymorphic as dictlike

metadata = MetaData()
Base = declarative_base()
engine = create_engine('sqlite://', echo=True)
Base.metadata.bind = engine
session = Session(engine)


class AnimalFact(dictlike.PolymorphicVerticalProperty, Base):
    """key/value attribute whose value can be one of several types"""
    __tablename__ = 'animalfacts'
    type_map = {#str: ('string', 'str_value'),
                list: ('list', 'list_value'),
                tuple: ('tuple', 'tuple_value')}
    id = Column(Integer, primary_key=True)
    animal_id = Column(Integer, ForeignKey('animal.id'), primary_key=True)
    key = Column(String, primary_key=True)
    type = Column(String)
    #str_value = Column(String)
    list_value = relationship('StringEntry')
    tuple_value = relationship('StringEntry2')


class Animal(Base, dictlike.VerticalPropertyDictMixin):
    __tablename__ = 'animal'
    _property_type = AnimalFact
    _property_mapping = 'facts'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    facts = relationship(AnimalFact, backref='animal',
                          collection_class=attribute_mapped_collection('key'))

    def __init__(self, name):
        self.name = name


class StringEntry(Base):
    __tablename__ = 'stringentry'
    id = Column(Integer, primary_key=True)
    animalfacts_id = Column(Integer, ForeignKey('animalfacts.id'))
    value = Column(String)

    def __init__(self, value):
        self.value = value


class StringEntry2(Base):
    __tablename__ = 'stringentry2'
    id = Column(Integer, primary_key=True)
    animalfacts_id = Column(Integer, ForeignKey('animalfacts.id'))
    value = Column(String)

    def __init__(self, value):
        self.value = value

Base.metadata.create_all()


a = Animal('aardvark')
a['eyes'] = [StringEntry('left side'), StringEntry('right side')]  # works great
a['eyes'] = (StringEntry2('left side'), StringEntry2('right side'))  # works great
#a['cute'] = 'sort of'  # failure
4

1 回答 1

3

PickleType 确实是一种解决边缘情况的 hacky 方法,在这种情况下,您有一些任意对象,您只想推开。可以肯定的是,当您使用 PickleType 时,您将放弃任何关系优势,包括能够过滤/查询它们等。

因此,将 ORM 映射对象放在 Pickle 中基本上是一个糟糕的主意。

如果您想要一个标量值的集合,请结合使用传统映射和 relationship() 与 association_proxy。请参阅http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/associationproxy.html#simplifying-scalar-collections

“或字典”。使用attribute_mapped_collection: http ://docs.sqlalchemy.org/en/rel_0_7/orm/collections.html#dictionary-collections

“字典加标量”:结合attribute_mapped_collection和association_proxy:http ://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/associationproxy.html#proxying-to-dictionary-based-collections

编辑1:嗯,你在那里挖掘了一个非常深奥和复杂的例子。Association_proxy 是一种更简单的方法来解决您希望对象像标量一样行为的这些情况,所以这就是,没有“垂直”示例的所有疯狂样板,我会避免使用它,因为它真的太复杂了。您的示例似乎未决定主键样式,因此我使用了复合版本。代理+复合不能混合在一个表中(它可以,但它的关系不正确。键应该是标识行的最小单位 - http://en.wikipedia.org/wiki/Unique_key是一个很好的顶级阅读有关此的各种主题)。

from sqlalchemy import Integer, String, Column, create_engine, ForeignKey, ForeignKeyConstraint
from sqlalchemy.orm import relationship, Session
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy

Base = declarative_base()

class AnimalFact(Base):
    """key/value attribute whose value can be either a string or a list of strings"""
    __tablename__ = 'animalfacts'

    # use either surrogate PK id, or the composite animal_id/key - but
    # not both.   id/animal_id/key all together is not a proper key.
    # Personally I'd go for "id" here, but here's the composite version.

    animal_id = Column(Integer, ForeignKey('animal.id'), primary_key=True)
    key = Column(String, primary_key=True)

    # data
    str_value = Column(String)
    _list_value = relationship('StringEntry')

    # proxy list strings
    list_proxy = association_proxy('_list_value', 'value')

    def __init__(self, key, value):
        self.key = key
        self.value = value

    @property
    def value(self):
        if self.str_value is not None:
            return self.str_value
        else:
            return self.list_proxy

    @value.setter
    def value(self, value):
        if isinstance(value, basestring):
            self.str_value = value
        elif isinstance(value, list):
            self.list_proxy = value
        else:
            assert False

class Animal(Base):
    __tablename__ = 'animal'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    _facts = relationship(AnimalFact, backref='animal',
                          collection_class=attribute_mapped_collection('key'))
    facts = association_proxy('_facts', 'value')

    def __init__(self, name):
        self.name = name

    # dictionary interface around "facts".
    # I'd just use "animal.facts" here, but here's how to skip that.
    def __getitem__(self, key):
        return self.facts.__getitem__(key)

    def __setitem__(self, key, value):
        self.facts.__setitem__(key, value)

    def __delitem__(self, key):
        self.facts.__delitem__(key)

    def __contains__(self, key):
        return self.facts.__contains__(key)

    def keys(self):
        return self.facts.keys()


class StringEntry(Base):
    __tablename__ = 'myvalue'
    id = Column(Integer, primary_key=True)
    animal_id = Column(Integer)
    key = Column(Integer)
    value = Column(String)

    # because AnimalFact has a composite PK, we need
    # a composite FK.
    __table_args__ = (ForeignKeyConstraint(
                        ['key', 'animal_id'],
                        ['animalfacts.key', 'animalfacts.animal_id']),
                    )
    def __init__(self, value):
        self.value = value

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

session = Session(engine)


# create a new animal
a = Animal('aardvark')

a['eyes'] = ['left side', 'right side']

a['cute'] = 'sort of'

session.add(a)
session.commit()
session.close()

for animal in session.query(Animal):
    print animal.name, ",".join(["%s" % animal[key] for key in animal.keys()])
于 2012-08-27T19:59:59.490 回答