繁体   English   中英

Azure表:没有空字符串属性(列)?

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

使用python SDK。 在我看来,不可能将空字符串作为属性的值存储在Azure表中。

例如(这是针对云的,而不是本地的):

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

结果是:

# ./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'

这使得到处都是难看的“ if hasattr”代码。 您不能假设您从该表中提取的每个实体都将具有middle_name属性。 这个例子很简单,但是假设有100个属性。

我已经尝试过”,“”,“”等。我也尝试过“无”。 这些都不会导致将属性添加到数据库。

正确/错误起作用。

我不了解Azure Tables的基本知识,因为在我看来我应该能够存储一个空字符串。 也许存储空值但存储空字符串违反了NoSQL福音?

您的理解是正确的。 Azure表存储中支持空字符串。 SDK似乎有问题。 如果您在GitHub查看用于序列化实体的源代码(请查找函数_convert_entity_to_xml 690行),则会注意到以下内容:

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

本质上,上面的代码正在检查值是否为空,然后通过将m:null属性设置为true来告诉表服务不要存储该属性。

如果我使用.Net SDK尝试使用与您相同的代码,那么它可以很好地工作(因此,有关Python SDK问题的注释)。

您可以做的是提交一个错误报告,负责管理此错误的团队应予以照顾。 除此之外,由于已经安装了SDK,因此还可以在本地副本上更改代码,以便代码处理空字符串。 就像是:

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

(我不是真的用Python编写代码,所以我相信您会找到一种更好的方法:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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