简体   繁体   中英

Azure Tables: no empty-string properties (columns)?

Working with the python SDK. It appears to me that it's impossible to store an empty string as a property's value in an Azure Table.

For example (and this is against the cloud, not local):

table_service.create_table('stackoverflow')

person = dict()
person['PartitionKey'] = 'something'
person['RowKey'] = '1'
person['first_name'] = 'John'
person['middle_name'] = ' '
person['last_name'] = 'Smith'

table_service.insert_entity('stackoverflow',person)

myEnt = table_service.get_entity('stackoverflow','something','1')

print "First name is " + myEnt.first_name
print "Middle name is " + myEnt.middle_name

This results in:

# ./test_azure.py 
First name is John
Traceback (most recent call last):
  File "./test_azure.py", line 25, in <module>
    print "Middle name is " + myEnt.middle_name
AttributeError: 'Entity' object has no attribute 'middle_name'

This makes for a lot of ugly "if hasattr" code all over the place. You can't assume that every Entity you pull from this table will have a middle_name property. This example is simple, but imagine that are 100 properties.

I've tried '', ' ', ' ', etc. I've also tried None. None of these result in a property being added to the database.

True/False do work.

Is there something fundamental about Azure Tables I'm not understanding, because it seems to me that I should be able to store an empty string. Maybe it's a violation of NoSQL gospel to store a null, but an empty string?

Your understanding is correct. Empty strings are supported in Azure Table Storage. It looks like there's an issue with the SDK. If you look at the source code for serializing an entity here on GitHub (look for function _convert_entity_to_xml , line 690), you will notice the following:

if value == '':
    properties_str += ' m:null="true" />'

Essentially what the code above is doing is checking if the value is empty, then it is telling table service not to store this attribute by setting m:null attribute to true .

If I try same code as yours using .Net SDK, it works perfectly fine (and thus the comment about issue with Python SDK).

What you could do is file a bug report and the team responsible for managing this should take care of it. Apart from that, since you have the SDK installed you could also make the code change on your local copy so that your code handles empty strings. Something like:

if value == '':
    if mtype != 'Edm.String':
        properties_str += ' m:null="true" />'

(I don't really code in Python so I'm sure you will find a better way of doing this :))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM