Empty dict-like collection problem in SQLAlchemy

Posted by maksymko on Stack Overflow See other posts from Stack Overflow or by maksymko
Published on 2010-03-23T08:27:43Z Indexed on 2010/03/23 8:33 UTC
Read the original article Hit count: 230

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about python

Related posts about sqlalchemy