我想在我的sqlite数据库中加载/保存一个dict,但是在找到一个简单的方法时遇到了一些问题.我真的不需要能够根据内容进行过滤等,因此可以简单地转换为/来自字符串.
下一个最好的东西是外键.请不要发布链接到巨大的例子,如果我盯着任何那些,我的头会爆炸.
我对SQLAlchemy有疑问.如何在我的映射类中添加类似字典的属性,该属性将字符串键映射到字符串值,并将存储在数据库中(与原始映射对象在同一个表或另一个表中).我希望这添加对我的对象的任意标记的支持.
我在SQLAlchemy文档中找到了以下示例:
from sqlalchemy.orm.collections import column_mapped_collection, attribute_mapped_collection, mapped_collection
mapper(Item, items_table, properties={
# key by column
'notes': relation(Note, collection_class=column_mapped_collection(notes_table.c.keyword)),
# or named attribute
'notes2': relation(Note, collection_class=attribute_mapped_collection('keyword')),
# or any callable
'notes3': relation(Note, collection_class=mapped_collection(lambda entity: entity.a + entity.b))
})
item = Item()
item.notes['color'] = Note('color', 'blue')
Run Code Online (Sandbox Code Playgroud)
但我想要以下行为:
mapper(Item, items_table, properties={
# key by column
'notes': relation(...),
})
item = Item()
item.notes['color'] = 'blue'
Run Code Online (Sandbox Code Playgroud)
在SQLAlchemy中有可能吗?
谢谢