When building out an application there are many factors you need to consider, these considerations boil down to functional and non-functional requirements that you as a developer or architect are set out to achieve. In many circumstances, if not all, we are accepting data as an input from a user or other medium, transforming or normalizing that data into something that will help fulfill other requirements of an application.

In order to fulfill scalability goals it may be necessary to find alternative means to traditional storage, something that was designed with scalability in mind. One such service is Windows Azure Table Storage which is defined as:

[Windows Azure Storage] Tables offer NoSQL capabilities for applications that require storage of large amounts of unstructured data. Tables are an ISO 27001 certified managed service which can auto scale to meet massive volume of up to 100 terabytes and throughput and accessible from virtually anywhere via REST and managed API’s.

While this doesn’t seem like a foreign concept you may find yourself in a situation where you have an error while attempting to use integers as strings while interacting with the Windows Azure SDK for PHP. The error you will receive is “PartitionKey [or RowKey] property value must satisfy is_string”. In my particular scenario I was loading my tables by iterating through an array of key/value pairs.

My array looked like this:

In which you may expect the key to be treated as a string, which isn’t the case. To understand the reasoning behind this, we’ll go right to the source.

The key can either be an integer or a string. The value can be of any type.

Additionally the following key casts will occur:

Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn’t a valid decimal integer.

Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.

Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.

Null will be cast to the empty string, i.e. the key null will actually be stored under "".

Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

According to the documentation, you’ll notice that “strings containing valid integers will be cast to integer type”, in order to ensure that the now converted integer is provided to the service proxy as a string, we need to call strval() on the key when providing it as a PartitionKey or RowKey. To provide an example of this consider the following:

Where addEntity() constructs an entity and inserts it into Windows Azure Table Storage.

Conclusion

In this post I clarified a potential issue that may arise when iterating over an array with key/value pairs to push entities into Windows Azure Table Storage.