如何使用SQLAlchemy将一个类映射到多个表? [英] How to map one class against multiple tables with SQLAlchemy?

查看:201
本文介绍了如何使用SQLAlchemy将一个类映射到多个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个数据库结构,有三个表格,如下所示:

  items 
- item_id
- item_handle

属性
- attribute_id
- attribute_name

item_attributes
- item_attribute_id
- item_id
- attribute_id
- attribute_value



我希望能够在SQLAlchemy :

  item = Item('item1')
item.foo ='bar'

session.add(item)
session.commit()

item1 = session.query(Item).filter_by(handle ='item1')。one()
print item1.foo#=> 'bar'

我是SQLAlchemy的新用户,我在文档中找到了这个( http://www.sqlalchemy.org/docs/ 05 / mappers.html#mapping-a-class-against-multiple-tables ):

  j = join (items,item_attributes,items.c.item_id == item_attributes.c.item_id)。 \ 
join(attributes,item_attributes.c.attribute_id == attributes.c.attribute_id)

mapper(Item,j,properties = {
'item_id':[items .c.item_id,item_attributes.c.item_id],
'attribute_id':[item_attributes.c.attribute_id,attributes.c.attribute_id],
})

它只会将Item_id和attribute_id添加到Item,并且不能向Item对象添加属性。



是我想用SQLAlchemy实现可能吗?是否有更好的方法来构造数据库以获得动态列的相同行为?

解决方案

a href =http://en.wikipedia.org/wiki/Entity-attribute-value_model =noreferrer> entity-attribute-value 模式。在SQLAlchemy示例目录下有一个示例: vertical / 。 / p>

如果你使用PostgreSQL,那么还有 hstore contrib模块可以存储字符串到字符串映射。如果你有兴趣,那么我有一些自定义类型的代码,可以使用它通过SQLAlchemy存储扩展属性。



存储自定义属性的另一个选项是将它们序列化为文本字段。在这种情况下,您将失去按属性过滤的能力。


Lets say that I have a database structure with three tables that look like this:

items
 - item_id
 - item_handle

attributes
 - attribute_id
 - attribute_name

item_attributes
 - item_attribute_id
 - item_id
 - attribute_id
 - attribute_value

I would like to be able to do this in SQLAlchemy:

item = Item('item1')
item.foo = 'bar'

session.add(item)
session.commit()

item1 = session.query(Item).filter_by(handle='item1').one()
print item1.foo # => 'bar'

I'm new to SQLAlchemy and I found this in the documentation (http://www.sqlalchemy.org/docs/05/mappers.html#mapping-a-class-against-multiple-tables):

j = join(items, item_attributes, items.c.item_id == item_attributes.c.item_id). \
    join(attributes, item_attributes.c.attribute_id == attributes.c.attribute_id)

mapper(Item, j, properties={
    'item_id': [items.c.item_id, item_attributes.c.item_id],
    'attribute_id': [item_attributes.c.attribute_id, attributes.c.attribute_id],
})

It only adds item_id and attribute_id to Item and its not possible to add attributes to Item object.

Is what I'm trying to achieve possible with SQLAlchemy? Is there a better way to structure the database to get the same behaviour of "dynamic columns"?

解决方案

This is called the entity-attribute-value pattern. There is an example about this under the SQLAlchemy examples directory: vertical/.

If you are using PostgreSQL, then there is also the hstore contrib module that can store a string to string mapping. If you are interested then I have some code for a custom type that makes it possible to use that to store extended attributes via SQLAlchemy.

Another option to store custom attributes is to serialize them to a text field. In that case you will lose the ability to filter by attributes.

这篇关于如何使用SQLAlchemy将一个类映射到多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆