Open wobeng opened 3 years ago
This is a problem for me too. I wanted to upgrade my PynamoDB version and start using DynamicMapAttribute, but several tests of mines are failing because of this behavior and I can't move forward.
The only thing I can add about this behavior is that it only happens with the update operations. The save() does not do had the attribute_values
in the DynamicMapAttribute.
+1 same for me
I did some investigation and found what is happening.
Basically, when we deserialize the object, the method _container_deserialize
gets called. The issue is that inside that function, it sets self.attribute_values = {}
to initialize it. Normally this works fine, but on MapAttribute
, it calls __setattr__
, which instead of "self.attribute_values = {}
" does self.attribute_values["attribute_values"] = {}
.
Going of the comment in __setattr__
:
# "Raw" (i.e. non-subclassed) instances set their name-value pairs in the `attribute_values` dictionary.
# MapAttribute subclasses should set attributes via the Attribute descriptors.
if self.is_raw() and self._is_attribute_container():
self.attribute_values[name] = value
...
I think the issue is in the implementation of the is_raw
method.
On MapAttribute
it looks like this:
@classmethod
def is_raw(cls):
return cls == MapAttribute
But on DynamicMapAttribute
it looks like this this:
@classmethod
def is_raw(cls):
# All subclasses of DynamicMapAttribute should be treated like "raw" map attributes.
return True
Hope this helps.
I also took some time to look into this and found the same issue as @fdobrovolny. I also found a fix that treats the "attribute_values" attribute as a special case in the __setattr__
method. However, technically it may break backwards-compatibility if anyone actually wants an attribute called "attribute_values". To be honest, I don't really see a way around this, though.
So it's not just me who gets this.
I have a use case where I keep a map of maps like so:
"myMap": {
"aba75878-c068-4f64-8559-ea62706b9c11": {
"foo": "bar",
"bar": "foo"
},
"eb6009b4-883e-46b9-a066-151e88f7c7d4": {
"foo": "bar",
"bar": "foo"
},
}
Basically a list of maps, but using a Map instead of a list. I could define each item with a MapAttribute
but my key names are just generated strings, so I'd need to use the DynamicMapAttribute
but that keeps adding this attribute_values
key, which is breaking my app.
Any idea what I could use to get around this?
Hello,
I have an attribute setup with way:
When I insert data and pull it back out I get an empty
attribute_values
map. Is it by design?Thank you so much in advance