Empty dict-like collection problem in SQLAlchemy
- by maksymko
I have a mapping in SQLAlchemy that looks like this:
t_property_value = sa.Table('property_value', MetaData, autoload = True, autoload_with = engine)
orm.mapper(PropertyValue, t_property_value)
t_estate = sa.Table('estate', MetaData, autoload = True, autoload_with = engine)
orm.mapper(Estate, t_estate, properties = dict(
property_hash = orm.relation(PropertyValue, collection_class = column_mapped_collection(t_property_value.c.property_id))
))
Now, everything seems to be fine, when I load the Estate object and it has some relations to PropertyValue objects. However, when it does not, then property_hash attribute is None, instead of being something dict-like, so I can not add new relations like this:
estate.property_hash[prop_id] = PropertyValue(...)
because I get the "'NoneType' object does not support item assignment" error.
So, is there any way to force SQLAlchemy to create proper empty collection?