2
我正在尝试使用sqlalchemy存储一些模拟测量结果(时间和值)。这里是相关的表格定义。如果有更明智的表格定义,我很乐意看到它。SQLalchemy:将列映射到不同的属性
from sqlalchemy import create_engine, schema, orm
engine = create_engine('sqlite:///:memory:', echo=True)
metadata = schema.MetaData(bind=engine)
container_table = schema.Table('containers', metadata,
schema.Column('id', schema.types.Integer, primary_key=True))
measurement_table = schema.Table('measurements', metadata,
schema.Column('id', schema.types.Integer, primary_key=True),
schema.Column('container_id', schema.types.Integer,
schema.ForeignKey('containers.id')),
schema.Column('time', schema.types.Float),
schema.Column('value', schema.types.Float))
metadata.create_all()
时间对每个容器都是唯一的,下面的属性应该按时间排序。
我希望能够读取和分配这些属性:
c = Container()
times = range(10)
values = [t**2 for t in times]
c.times = times
c.values = values
但我不知道该怎么办的映射。我认为如果可能的话,它会看起来像这样:
class Container(object):
times = some_sort_of_proxy()
values = some_sort_of_proxy()
orm.mapper(Container, container_table, properties={
# Magic
})
我该如何去做这件事?这是一个合理的映射,还是我需要有一个不同的基础表结构?
我知道属性如何工作。问题的关键是我的当前(基于属性)实现创建了很多不需要的对象(x-y对元组)。我认为最好的解决方案实际上是使用一个blob,我只是没有到处发布我自己的答案。 – Mark 2011-04-02 16:00:23