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')
しかし、次の動作が必要です。
mapper(Item, items_table, properties={
# key by column
'notes': relation(...),
})
item = Item()
item.notes['color'] = 'blue'
SQLAlchemyで可能ですか?
ありがとうございました